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

  Рет қаралды 6,866

Flutter Mentor

Flutter Mentor

Күн бұрын

Пікірлер: 26
@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.
@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
@leleemagnu6831
@leleemagnu6831 2 жыл бұрын
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!
@StoreRunDotCom
@StoreRunDotCom 26 күн бұрын
yeah I am still fuzzy but at least I got that its about identifying where something exists relative to everything. I loved how you used the metaphor of waking up hungover and having to figure out where to go LOL - Merry Christmas
@mavie--
@mavie-- Жыл бұрын
Ty, muito obrigado! me ajudou demais a entender o conceito de context!!!!
@aravindhkumar69
@aravindhkumar69 Жыл бұрын
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
@erfansoltanzadeh-u8x
@erfansoltanzadeh-u8x Жыл бұрын
its pretty good thank you for making this
@demodemo9429
@demodemo9429 2 жыл бұрын
Great explaination...
@lammmpo
@lammmpo 2 жыл бұрын
Really nice man
@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 KZbinr 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
@vfthome-yt
@vfthome-yt Жыл бұрын
THANK YOU!
@asheshshrestha9043
@asheshshrestha9043 3 жыл бұрын
very well explained. Thanks a lot
@FlutterMentor
@FlutterMentor 3 жыл бұрын
Glad it was helpful!
@dantedt3931
@dantedt3931 3 жыл бұрын
Thanks!
@FlutterMentor
@FlutterMentor 3 жыл бұрын
No problem!
@MachineLearning-rw7jd
@MachineLearning-rw7jd Жыл бұрын
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.
What is BuildContext | Decoding Flutter
12:23
eTechViral
Рет қаралды 1,5 М.
Who is More Stupid? #tiktok #sigmagirl #funny
0:27
CRAZY GREAPA
Рет қаралды 10 МЛН
OCCUPIED #shortssprintbrasil
0:37
Natan por Aí
Рет қаралды 131 МЛН
Какой я клей? | CLEX #shorts
0:59
CLEX
Рет қаралды 1,9 МЛН
Flutter - Widget Properties Explained (For Programming Beginners)
8:07
Everything About Flutter Themes In Less Than 10 Minutes
9:38
Flutter Mentor
Рет қаралды 42 М.
Understanding BuildContext in Flutter
12:26
Garrett Love
Рет қаралды 32 М.
PrimaryScrollController | Decoding Flutter
8:07
Flutter
Рет қаралды 52 М.
Buildcontext in Flutter | Google Developers North America
12:15
Google Developer Communities North America
Рет қаралды 1,4 М.
BuildContext - Flutter
4:02
Hungrimind
Рет қаралды 10 М.
Flutter framework code review: BuildContext & Element
1:10:42
creativecreatorormaybenot
Рет қаралды 6 М.
Synchronous BuildContexts | Decoding Flutter
6:15
Flutter
Рет қаралды 47 М.
Who is More Stupid? #tiktok #sigmagirl #funny
0:27
CRAZY GREAPA
Рет қаралды 10 МЛН