Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,780 --> 00:00:05,310
In the last section, we spoke about some of the weird syntax right here, we're going to now have a
2
00:00:05,310 --> 00:00:11,310
very quick discussion and talk about why we had such a painful refactor to take our app over to a stateful
3
00:00:11,310 --> 00:00:11,720
widget.
4
00:00:12,120 --> 00:00:15,540
In other words, why did we have to create two separate classes right here?
5
00:00:15,930 --> 00:00:19,440
And why did our App Widget have to have a create state method tied to it?
6
00:00:19,920 --> 00:00:22,980
Well, let's take a look at a diagram that's going to help us understand what's going on.
7
00:00:23,760 --> 00:00:30,510
OK, so this is a flow diagram of what would happen if we did not did not have a stateful widget.
8
00:00:30,720 --> 00:00:31,290
That's the key.
9
00:00:31,320 --> 00:00:35,760
OK, so this is how life would be if we did not have stateful widgets.
10
00:00:36,660 --> 00:00:37,890
So let's walk through the flow here.
11
00:00:38,370 --> 00:00:43,740
At the very start, Flutter would decide to look at our app and render our widget on the screen of our
12
00:00:43,740 --> 00:00:44,250
device.
13
00:00:44,850 --> 00:00:48,270
Now will assume that our state lists widget of app right here.
14
00:00:48,330 --> 00:00:50,130
Again, this is considering the case.
15
00:00:50,130 --> 00:00:54,240
We did not have a stateful widget would create a new instance of our app widget.
16
00:00:55,140 --> 00:00:58,610
That app widget might have a counter variable assigned to it of zero.
17
00:00:58,980 --> 00:01:02,400
And so that new app widget would display the number zero on the screen.
18
00:01:03,280 --> 00:01:08,830
So then maybe at some point in time, a user presses that button and that increments the value of counter
19
00:01:08,830 --> 00:01:09,430
to one.
20
00:01:10,120 --> 00:01:11,560
Now, here's the key part.
21
00:01:12,570 --> 00:01:19,860
If Flutter then decided to re render our application, it would create a new instance of our app widget,
22
00:01:20,700 --> 00:01:24,780
so we would now have two instances of the app class and play.
23
00:01:25,560 --> 00:01:32,370
We have the old copy that now has a counter value of one assigned to it, but flutter when it re rendered
24
00:01:32,370 --> 00:01:38,760
our application, created a second instance of app, and that second instance has the initial value
25
00:01:38,760 --> 00:01:40,890
of counter assigned to it of zero.
26
00:01:43,070 --> 00:01:48,740
Flutter throws away the old copy of app, and it decides to now use the new one to represent our app
27
00:01:48,740 --> 00:01:50,480
widget on the screen of the device.
28
00:01:51,150 --> 00:01:57,590
And so when Fluttery renders our application with this new copy of app, we still have the zero value
29
00:01:57,590 --> 00:01:58,280
of counter.
30
00:01:59,250 --> 00:02:01,500
And so we still see zero on the screen.
31
00:02:02,630 --> 00:02:08,750
Now, the real key here, the key thing to understand is why Flutter decides to create a new instance
32
00:02:08,750 --> 00:02:09,770
of our app widget.
33
00:02:10,100 --> 00:02:14,890
And the best way I can show you that is by pointing out the build method right here.
34
00:02:15,560 --> 00:02:20,810
So whenever Flutter tries to render our application, it's wouldn't you call the built method on every
35
00:02:20,810 --> 00:02:22,780
widget that we have inside of our application.
36
00:02:23,480 --> 00:02:28,190
When you look at this build method, you'll notice that every single widget that we create inside of
37
00:02:28,190 --> 00:02:30,710
here is created from scratch.
38
00:02:31,370 --> 00:02:35,700
So material app right here is a new instance of the material app class.
39
00:02:36,140 --> 00:02:41,720
This is a new instance of scaffold, a new instance of text, a new instance of floating action button.
40
00:02:42,590 --> 00:02:48,080
So every time that flutter decides to re render our application, it is creating a new instance of these
41
00:02:48,080 --> 00:02:48,620
widgets.
42
00:02:48,920 --> 00:02:53,570
So all the old instances get thrown away and we create these new ones.
43
00:02:54,380 --> 00:03:00,500
And when we get this new instance of this widget, it comes with a completely reinitialize set of instant's
44
00:03:00,500 --> 00:03:01,190
variables.
45
00:03:01,970 --> 00:03:06,830
So if we did not have stateful widgets, we would never see any changes appear on the screen of our
46
00:03:06,830 --> 00:03:07,310
device.
47
00:03:09,480 --> 00:03:13,980
OK, so let's now talk about what's happening, thanks to the fact that we do have stateful, which
48
00:03:13,980 --> 00:03:16,590
is, you know, what's happening now that we have these two separate classes.
49
00:03:17,090 --> 00:03:17,400
All right.
50
00:03:17,400 --> 00:03:19,160
So slightly different flow diagram here.
51
00:03:19,620 --> 00:03:21,540
So this is with stateful widgets.
52
00:03:22,570 --> 00:03:27,190
So we start off again with flutter, deciding to render our app on the screen, not the device.
53
00:03:28,660 --> 00:03:33,430
It creates a new instance of the app widget, so here's the app widget right here, like that's the
54
00:03:33,430 --> 00:03:35,590
top class, this one right here.
55
00:03:38,390 --> 00:03:43,250
When that widget is created, when the output is created, flutters, then going to automatically call
56
00:03:43,250 --> 00:03:46,000
the create state method on that widget.
57
00:03:46,160 --> 00:03:52,880
So the state method that you and I defined, so that's create state right here inside of create state,
58
00:03:52,910 --> 00:03:57,830
we create a new instance of app state and return it from that method.
59
00:03:59,510 --> 00:04:05,390
Flutter is going to take that app state object that we just created and it cashes it, so it holds onto
60
00:04:05,390 --> 00:04:12,620
it and it does not throw it away, then whenever a user presses that button, the counter will increment
61
00:04:12,620 --> 00:04:13,130
by one.
62
00:04:13,910 --> 00:04:20,329
And because you and I called set state inside of our app, State Widget Flutter decides to render our
63
00:04:20,329 --> 00:04:20,990
application.
64
00:04:21,970 --> 00:04:27,640
So at that point, a new instance of the app widget is created just as it did before, over on this
65
00:04:27,640 --> 00:04:28,150
diagram.
66
00:04:29,400 --> 00:04:36,870
But the old copy of our state is kept around, so we are not creating a new copy of App State because
67
00:04:36,870 --> 00:04:42,930
that app State Object gets cached and it sits around no matter how many times Flutter decides to render
68
00:04:42,930 --> 00:04:43,740
our application.
69
00:04:44,910 --> 00:04:49,680
Because upstate does not get Virender or something, because upstate does not get recreated, its instance
70
00:04:49,680 --> 00:04:54,810
variables do not get reset or they don't get initialized back to their starting values.
71
00:04:55,650 --> 00:05:01,890
So we then re render our app widget, but it uses this original copy of app states where the counter
72
00:05:01,890 --> 00:05:03,660
value is still set to one.
73
00:05:05,690 --> 00:05:10,770
I think another diagram that might be helpful to take a quick glance at is this one right here.
74
00:05:11,030 --> 00:05:12,530
So here's our widget tree again.
75
00:05:13,070 --> 00:05:15,080
At the very top, we've got that app widget.
76
00:05:15,260 --> 00:05:18,700
So that's the original one right here, the first class we put together.
77
00:05:20,390 --> 00:05:26,540
And any time the app is first created, we produce a new instance of App State through the create state
78
00:05:26,540 --> 00:05:26,950
method.
79
00:05:27,740 --> 00:05:32,660
So this widget right here, that first class we put together, this thing is going to be thrown away,
80
00:05:32,660 --> 00:05:35,900
recreated many times in the life cycle of our application.
81
00:05:36,500 --> 00:05:42,710
But the app state object over here only created one time and then we hold on to it for as long as our
82
00:05:42,710 --> 00:05:46,630
app is running or until we decide that we don't want to render this widget on the screen anymore.
83
00:05:47,840 --> 00:05:52,880
OK, so hopefully that gives you a better idea of why we have to create two classes right here, it's
84
00:05:52,880 --> 00:05:57,710
all because whenever Flutter decides to render our application, it's going to throw away this widget
85
00:05:57,710 --> 00:05:59,480
and it's going to try to recreate it.
86
00:06:00,410 --> 00:06:04,600
If we recreated it, then we would lose all the data that we had tied to that widget previously.
87
00:06:05,150 --> 00:06:10,730
So by making sure that we have this great state method right here, along with a separate app state
88
00:06:10,970 --> 00:06:16,310
class, we're going to hold on to our instance of the state that has created, no matter how many times
89
00:06:16,310 --> 00:06:17,600
our application is rendered.
90
00:06:18,630 --> 00:06:22,980
All right, so I think we got a better idea of what's going on here, so let's take a quick pause and
91
00:06:22,980 --> 00:06:24,390
we'll continue in the next section.
9276
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.