Flutter Tutorial for Beginners #7 - Stateless Widgets & Hot Reload
Key Takeaways
Explains stateless widgets and hot reload in Flutter
Full Transcript
alright then so right now when we make any kind of change to our app in the code over here and we want to preview that change what we have to do is go down to this run tab down here and then click on this hot restart button and then that essentially refreshes the app over here so we can see that change now that's ok but we're making changes quite a lot and we have to keep going down here and pressing the refresh button and it would be nice if there was a simpler way now flutter also comes with something called hot reload meaning whenever we make a change and save it over here the app can auto reload in the preview screen now right now that's not working and that's because of how we're structuring our code over here in order for us to use hot reload we're gonna have to talk about something called stateless widgets so what I'm gonna do is come down to the bottom over here and I'm going to create a stateless widget first of all now we can do that using a little snippet in flutter just st which stands for states and then less and then tab and it creates this class and this is going to be a custom stateless widget class so we're gonna give this a name I'm just gonna call this test and what we're doing is just creating a class called test which extends stateless widget so in essence we're making our own custom stateless widget here now pretty much everything remember in flow to every widget is just a class so when we're using these things up here like the text widget the floating action button widget the textile widget is Center widget these are just instances of those classes in a flutter these are all just widget classes now these are all built into the framework what we're doing down here is basically making our own widget class and that's extending the base stateless widget class in flutter so we're inheriting all of those call functionalities in stateless widgets now I keep using the word stateless when I talk about this widget but what does that mean exactly well in flutter we can have either stateless or stateful widgets right now we've created a stateless widget and that basically means that the state of the widget cannot change over time for example the layout or the colors or any data we use inside that widget has to be final and it cannot change over time as we use the app it can contain data but that data can't change after the widgets been initialized stateful widgets on the other hand they can contain States which can change over time so things like it's call it's layout or any data inside it it can change over time so for example some kind of counting widget that displays the number of flies that you SWAT on an app that would have changing data over time as just what more flies on the screen the number would go up right so we would use a stateful widget for that so depending on what we need in our apps for our different parts of it our different widgets we'd either choose a stateless widget or a state full of widgets right now we're using a stateless widget but later on in the course we will also look at stateful widgets as well okay then so at the minute we're creating this stateless widget right here which is called test and I'm gonna rename that home because ultimately this is going to represent all of the content we're going to show on the home screen and this is extending stateless widget which means we can't have any state that changes over time inside this widget now I mentioned that the reason we're doing this well one of the reasons we're doing this is because it's going to enable hot reload for us and we'll take a look at that in a minute but another reason we're going to do this is because it's going to make our code drier and much more reusable we can easily reuse our own custom widgets later on and we'll see that in action later in the course but anyway how does this stateless widget help with hot reload well first of all we have inside it this build function right here and we can see the return type in front of the function is a widget and that's what we're doing inside this function we're returning this container widget right here now we've not seen the container widget but don't worry about that for now all we're doing is returning a widget now ultimately what I want to do inside this widget is return a widget tree of our homescreen now we've already kind of created that over here we've done it directly inside the material up inside the home property so what I'm going to do is just copy or rather cut all of this scaffold stuff everything inside it I'm gonna cut it so we're just left with that home property and I'm gonna return it inside this build function so now we're returning this widget tree which starts with the scaffold and all of the different widgets inside it nested now at the bottom I just need to get rid of this comma because now we're not adding another widget after it we just replace that with the semicolon because remember we're returning something here and at the end of a return statement we have a semicolon anyway now we're returning this widget tree but back to this build function how is this helping goes with the hot reload problem well this build function right here this is what is responsible for building up the widget tree inside the stateless home widgets so all of this stuff right here now whenever we make a change to the code inside this widget tree flutter is going to detect that when we save it and it's going to cause the build function to rerun now when this reruns flutter is going to update what we see in the screen over here that's hot reload in action now it doesn't mean to rebuild the whole app that we create just where the code changes inside it so in the future we could have many different stateless widgets and if we make a change to one of them inside our code flutter only needs to rebuild that widget for us and update that on the screen so that's going to result to a much quicker update in the device preview so we don't have to go down here now to run and then Hawtree start to see that in action so let's do now a little change all I'm going to do is just come down to here and say click me and press save and now if we do this then we have an issue so let's go down here and okay I know why this is it's because we're not actually using this home widget we've built this widget but we're not actually using it anywhere and what we want to do is use it for the home screen in the material app so let me now do that and we're saying okay well for the home screen we want to use this custom home widget that we've created right here which is returning this widget tree so now all of this should show on the home screen so let me now save this and now it's reloading and now what we to begin with is one quick hot restart first of all just to capture any changes then you can see click me now if I take that off if I change something inside this widget and save it now it should automatically reload over here we don't have to go to run and then hop restart again because we may be changed inside this build function right here and it's detected that change its rerunning the build function and then updating that over here that is hot reload in action now I said that this makes our code drier when we use our own custom widgets like this and it does and that's because we can now reuse this home widget anywhere else in our app if we want to we might not necessarily do that because we only have one home screen but if we made up a widget that we want to use in several different places on different screens in our app we could then reuse this same widget every time we need it instead of rewriting out the code for every place we just put it in a widget like this and then we use an instance of that widget when we need it like this so that's nice now there's one more thing I want to talk about in this video and that's this override thing right here now this is used to say that this build function right here will override the one defined in the classes ancestor so the thing that we extend from which is stateless widget because that has its own build function as well so we're saying we want to use this build function because we're using this override thing right here we want to use this build function instead of the one we initially inherit from this stateless widget that's all this means we're redefining the build method right here so now we've done that whenever we make a change we don't need to click our hot restart anymore well we will be using that later on when we start to use state and we want to reset that state or data in our app but for now we don't need to worry about coming down here and clicking this every time we make a change
Original Description
Hey gang, in this Flutter tutorial I'll talk about what stateless widgets are and how we can begin to make our code more modular by using them. We'll also see how they help us with a feature called hot reload, to help speed up development.
----------------------------------------
🐱💻 🐱💻 Course Links:
Course files - https://github.com/iamshaunjp/flutter-beginners-tutorial
Android Studio - https://developer.android.com/studio
Git - https://git-scm.com/downloads
Flutter Installation - https://flutter.dev/docs/get-started/install
🐱💻 🐱💻 Other Related Courses:
+ Modern JavaScript Tutorial - https://www.udemy.com/modern-javascript-from-novice-to-ninja/?couponCode=NINJAYT
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
Related Reads
📰
📰
📰
📰
AI-Ready Infrastructure: A Checklist Before You Scale
Dev.to AI
Your Website Passed Claude’s Search Test. It Might Still Fail the Other Two
Medium · AI
AI Made Creation Free. Taste Is What's Scarce Now
Forbes Innovation
‘Football Manager’ Spin-Off Could Help Clubs Find The Next Messi
Forbes Innovation
🎓
Tutor Explanation
DeepCamp AI