Тёмный

Flutter - BuildContext Explained For Beginners (+ Bonus: How To Use SnackBar) 

Flutter Mentor
Подписаться 6 тыс.
Просмотров 6 тыс.
50% 1

If you're unsure of what the BuildContext is, then this is the video that will clear your doubts.
The concept of BuildContext itself is actually very simple: it lets the widgets (that need it) know where in the widget tree they are placed, so that they can do its function correctly.
But sometimes, you need to pass on a different context than the one in the arguments of the build method. And this video should clear your doubts right up.
If you still don't know what it is at the end of the video, watch it again! And I promise it'll click for you.
It is definitely a must to be able to understand what BuildContext is and how you should use it in your Flutter journey. So, click that play button and drop a like if you understand the BuildContext after this video tutorial!
BuildContext Class Documentation: api.flutter.dev/flutter/widge...
00:00 - Intro
00:27 - Showing UI/Code
00:40 - BuildContext Class Documentation
01:22 - Basic Explanation (With Analogy)
01:37 - Summing It Up
02:04 - Explaining It Using a Widget Tree
02:46 - Explaining The Documentation Example
03:14 - Using BuildContext
04:53 - Making The SnackBar Work
07:10 - Like & Subscribe
07:18 - Flutter Mentor Out
#BuildContext #Flutter #SnackBar
Credits:
Drunk Man Image:
pngtree.com/so/man-clipart (clipart png from pngtree.com)
OUTRO SONG:
Mitsubachi by Smith The Mister smiththemister.bandcamp.com
Smith The Mister bit.ly/Smith-The-Mister-YT
Free Download / Stream: bit.ly/mitsubachi
Music promoted by Audio Library • Mitsubachi - Smith The...

Опубликовано:

 

16 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 25   
@FlutterMentor
@FlutterMentor 3 года назад
Meanwhile, with Flutter 2.0 the SnackBar code updated. This is how to invoke the showSnackBar method in Flutter 2.0 (my next video is about this kind of deprecations): ScaffoldMessenger.of(context).showSnackBar() (Yes, basically just add "Messenger" after "Scaffold")
@danielhalmstrand2123
@danielhalmstrand2123 3 года назад
Thank you again, I am not concerned with snackbar itself. I just wanted to understand that any widget using Context to be drawn should not run on the same class as scaffold.
@leleemagnu6831
@leleemagnu6831 Год назад
wow! Hard concept to grasp. I watched 5 videos prior this one and i got more confused if anything ... your video was a revelation. Thank you ... not so hard when thigs are well explained. Truly thank you, fantastic video great explanation!
@Mark-mo4bo
@Mark-mo4bo 3 года назад
Thanks man! Surprisingly few explanations of this out there! Watching the pros bashing out builders under widgets without knowing why finally makes sense. Nice one.
@FlutterMentor
@FlutterMentor 3 года назад
Thanks for watching bro! And I appreciate the feedback as well, glad I could help
@mavie--
@mavie-- 9 месяцев назад
Ty, muito obrigado! me ajudou demais a entender o conceito de context!!!!
@demodemo9429
@demodemo9429 Год назад
Great explaination...
@VitorFThome
@VitorFThome Год назад
THANK YOU!
@lammmpo
@lammmpo Год назад
Really nice man
@user-rp7xx7xo2z
@user-rp7xx7xo2z Год назад
its pretty good thank you for making this
@aravindhkumar69
@aravindhkumar69 11 месяцев назад
The following assertion was thrown while handling a gesture: Navigator operation requested with a context that does not include a Navigator. The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget. When the exception was thrown, this was the stack: how to resolve this error
@asheshshrestha9043
@asheshshrestha9043 2 года назад
very well explained. Thanks a lot
@FlutterMentor
@FlutterMentor 2 года назад
Glad it was helpful!
@dantedt3931
@dantedt3931 2 года назад
Thanks!
@FlutterMentor
@FlutterMentor 2 года назад
No problem!
@danielhalmstrand2123
@danielhalmstrand2123 3 года назад
Hello and thank you for your explanations. So as I have clearly understood it, if a snackbar to work on first scaffold, it should not call scaffold.of(context) as it is calling on the scaffold itself, unless it is located in subtree beneth.
@FlutterMentor
@FlutterMentor 3 года назад
Yes. In this specific case, the SnackBar widget needs to use a context that CONTAINS a Scaffold (a context that has a scaffold). I say in this specific case, because the SnackBar widget specifically needs a context that contains a Scaffold. However, in most cases, you can just use the context that comes from the class (the one that comes with the method - build(BuildContext context)). So, most of the time that you use a context with a widget, the already given context (from the class) will work just fine because most widgets just want to know where in the widget tree they are located. But if you run into an error like this, where the widget or function you're trying to use wants a specific kind of context, now you can understand better what that error means. Let me know if this made it clearer or if I just confused you more lol
@danielhalmstrand2123
@danielhalmstrand2123 3 года назад
@@FlutterMentor Cheers mate that is clear now...much appreciated...best regards...
@FlutterMentor
@FlutterMentor 3 года назад
@@danielhalmstrand2123 my pleasure, thank you for watching!
@skirllexrude8170
@skirllexrude8170 3 года назад
I had some confusion.first every widget has its own context like scaffold,material app,elevated button etc.I understand that context are just to know where I am on this widget tree like myapp-> materialapp->scaffold->container etc. My app class becomes widget while instantiated right? And do every widget has build method for example scaffold widget has build method.And finally where do we use class build method context inside the widget tree.If it doesn't work for snackbar then what is the usecase of this context.Its just a parent context (top-level) of all context
@FlutterMentor
@FlutterMentor 3 года назад
Technically, all widgets do have their own context because they extend classes that have the build method, but don't worry about that. It's not usually very relevant to be aware that some button has its own build context, and you will probably never need to access it. So, if you're confused by that, don't worry about it. The Scaffold is a different, much more important widget because it's like the blueprint/foundation of your UI. But widgets essentially use context to get information on themselves and on the rest of the widget tree. On this example, the Builder widget has its own context and you can access to it because that's one of the reasons why Builder exists. The thing with the SnackBar is that it needs a context with a Scaffold ancestor (as in, a context within a Scaffold, not before it). And the context that you get from the Class isn't the same. One is created before the return statement (the class') and the other after the return statement (the scaffold's). That is why when I used my own custom Button class/widget instead of the Builder, it still worked. The context belonged to the Button class, but it was within the Scaffold, so it did have a Scaffold ancestor. A class is a stateful/stateless widget ONLY if it's extending the StatelessWidget or StafulWidget classes. If it does extend, then yeah you can always consider it a widget. Instantiated or not. Also, the reason why all widgets have build methods, is because, again, they're extending the StatelessWidget or StatefulWidget classes. It's within those 2 classes that the build method is present. Once you create a widget and extend those classes, then notice how it always says "override" before the build method. That's because you're overriding the method from the class you extended. And for the final question: yeah, the class context doesn't work for the SnackBar specifically, but you will see that you will use the context from the class 99% of the time, so that's why it's there. You use it in many different opportunities, it's hard to say when/where. But basically, if you're really new to Flutter, always try to use the class context first. It will work most of the time. Then if you get an error you try other solutions. Let me know if I missed something or if you're still confused! (sorry for taking long to get back to you)
@skirllexrude8170
@skirllexrude8170 3 года назад
@@FlutterMentor You are a life saver.Nevermind for the late reply.I really appreciate that you had spent your time on explaining everything.Now I had clear understanding of Context.Its not much important to dig deeper but knowing the foundation helps in future.Never saw such a crystal clear explanation from anyone like you.Saw some other RU-vidr like Johannes, santos but that doesn't makes sense.Today I was totally pleased with your answer.I think I will poke you again in future .Thanks My Mentor
@FlutterMentor
@FlutterMentor 3 года назад
@@skirllexrude8170 thank you for the feedback! I'm glad I could help that's what I'm here for. Good luck in your journey
@MachineLearning-rw7jd
@MachineLearning-rw7jd 7 месяцев назад
That’s a more practical example. I appreciate it. But there’s too much I can’t understand lol.
@fredgotpub871
@fredgotpub871 Год назад
I 've just tried to do print(' ${context.toString()} '). Funny.
Далее
impossible to understand how😨❓
00:14
Просмотров 2,5 млн
Understanding BuildContext in Flutter
12:26
Просмотров 31 тыс.
What is BuildContext | Decoding Flutter
12:23
BuildContext - Flutter
4:02
Просмотров 9 тыс.
35 Flutter Tips That Will Change Your Life
10:53
Просмотров 296 тыс.