Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,290 --> 00:00:07,660
So it would be great if we could let the user know that we're waiting for something to finish here before
2
00:00:07,660 --> 00:00:15,480
we actually leave that page and go back to that previous page. Put in other words, in our edit product
3
00:00:15,480 --> 00:00:16,500
screen,
4
00:00:16,500 --> 00:00:23,500
we only want to call pop here once we're really done adding this but until we're done, we want to show
5
00:00:23,500 --> 00:00:27,780
some loading spinner, some progress indicator. Now
6
00:00:27,790 --> 00:00:30,420
how could we do that?
7
00:00:30,440 --> 00:00:38,150
Well it would be awesome if add product here would return a future because if this would return a future,
8
00:00:38,480 --> 00:00:43,610
then we could use the same then trick here to wait for this future to be done,
9
00:00:43,640 --> 00:00:50,420
so for the response to have arrived before we call pop and it's pretty easy to make that work. Right
10
00:00:50,450 --> 00:00:50,770
now
11
00:00:50,780 --> 00:00:57,020
our add product method here in our products class here in the products.dart file returns nothing, it returns
12
00:00:57,020 --> 00:01:04,550
void. Well we can let it return a future and that future is a generic type where you typically indicate
13
00:01:04,580 --> 00:01:08,710
which kind of data that future will resolve to once it's done
14
00:01:08,750 --> 00:01:09,880
and here we don't really care,
15
00:01:09,890 --> 00:01:11,330
we just care that it's done,
16
00:01:11,330 --> 00:01:16,660
we don't want to pass any data back to our widget, so we can resolve to void,
17
00:01:16,670 --> 00:01:18,150
so to nothing.
18
00:01:18,710 --> 00:01:22,190
But now of course, we're not fulfilling this condition,
19
00:01:22,190 --> 00:01:26,140
we're saying here that we return a future but add product doesn't return anything,
20
00:01:26,180 --> 00:01:28,490
we have no return statement in here.
21
00:01:28,490 --> 00:01:34,970
So indeed now, we should return something and actually, you could think that you now return something
22
00:01:34,970 --> 00:01:35,600
here
23
00:01:35,750 --> 00:01:43,540
after notifying listeners, maybe future value like this. Theoretically, that returns a future which returns
24
00:01:43,540 --> 00:01:51,010
nothing or which resolves to nothing but you're returning inside of this anonymous function here, not inside
25
00:01:51,040 --> 00:01:52,980
of your add product method.
26
00:01:53,200 --> 00:01:59,080
Returning inside of a nested anonymous function like we're doing it here will not return from your main
27
00:01:59,080 --> 00:01:59,730
method,
28
00:01:59,860 --> 00:02:01,380
so this will not work,
29
00:02:01,390 --> 00:02:05,860
you're returning from the wrong function, you need to return outside of that.
30
00:02:05,860 --> 00:02:10,230
The problem with returning outside of that nested function just is that
31
00:02:10,270 --> 00:02:11,590
when you return here,
32
00:02:14,320 --> 00:02:21,020
you instantly return because you learned that Dart will basically execute this whole code snippet here,
33
00:02:21,040 --> 00:02:27,960
send off the request, register this function as a to-do function in the future but not wait for that and
34
00:02:27,970 --> 00:02:29,770
immediately move on to the next line,
35
00:02:29,770 --> 00:02:33,250
so this will be sent before this function ran.
36
00:02:33,280 --> 00:02:39,220
This function will still execute later because Dart did save it as an I have to execute this in the future
37
00:02:39,220 --> 00:02:41,730
function but it won't wait for that,
38
00:02:41,800 --> 00:02:43,700
so you return to early then.
39
00:02:43,900 --> 00:02:45,820
So what's the solution?
40
00:02:45,820 --> 00:02:54,460
Well we return this whole block here because post returns a future and with then, we register some code
41
00:02:54,490 --> 00:02:57,000
that should execute when that future resolves
42
00:02:57,400 --> 00:03:03,310
but then itself returns another future.
43
00:03:03,310 --> 00:03:07,210
We're not doing anything with that future but we could if we add another then block here
44
00:03:07,210 --> 00:03:12,990
but we don't need to, we have no other option or no other operation we want to do after this operation
45
00:03:13,510 --> 00:03:20,470
but since then returns another future, that's effectively what we return here overall because we
46
00:03:20,470 --> 00:03:25,990
return the result of calling post and then calling then and the result of calling then is another
47
00:03:25,990 --> 00:03:27,620
future and that's the future
48
00:03:27,640 --> 00:03:31,600
we return here.
49
00:03:31,670 --> 00:03:39,080
So with that, we effectively return a future now in add product and that future will resolve as soon
50
00:03:39,080 --> 00:03:40,610
as this then block is done
51
00:03:40,610 --> 00:03:42,000
which in turn is the case
52
00:03:42,080 --> 00:03:45,890
as soon as we have a response and we stored our product.
53
00:03:45,890 --> 00:03:54,260
So now we can go back to the edit product screen and here after add product, we now get back a future,
54
00:03:54,580 --> 00:03:55,820
here it is,
55
00:03:55,820 --> 00:03:58,670
so now we can call then here as well
56
00:03:58,880 --> 00:04:05,540
and inside of then, we now pass in a function where we now can call navigator pop to only pop
57
00:04:05,540 --> 00:04:06,500
once we're done.
58
00:04:06,740 --> 00:04:13,640
So instead of doing it down there, I do it immediately after updating because we're not sending HTTP requests
59
00:04:13,670 --> 00:04:14,480
there yet
60
00:04:14,720 --> 00:04:21,000
but if we're adding, I'm sending my pop request inside of this function which I pass to then.
61
00:04:21,080 --> 00:04:22,280
Now just one thing,
62
00:04:22,310 --> 00:04:28,920
even though our future in the end resolves to void and doesn't give us anything, we still have to accept
63
00:04:28,920 --> 00:04:32,030
this void argument here but we can't work with it.
64
00:04:32,090 --> 00:04:36,230
That's just some syntax, we get a value here which essentially is no value,
65
00:04:36,260 --> 00:04:42,080
that's just a Dart specialty, so we can just ignore this, it's just important to know that you have to
66
00:04:42,080 --> 00:04:46,280
accept a value here in this function you're getting executed,
67
00:04:46,280 --> 00:04:52,350
this will not hold any data though. Important for us is that now we call navigator of inside of this
68
00:04:52,400 --> 00:04:58,050
function which we pass to then and therefore we will only pop once this was stored.
69
00:04:58,210 --> 00:05:02,150
Now to also shows some loading indicator until this is done,
70
00:05:02,150 --> 00:05:10,010
we can go up to all the lists of properties we initialize and there, add a new property, isLoading which
71
00:05:10,010 --> 00:05:16,090
initially is false and we want to set that to true now
72
00:05:16,270 --> 00:05:24,580
once we submit our form, so maybe here once we call save here we set isLoading to true and wrap that
73
00:05:24,580 --> 00:05:31,030
into a set state call because I want to reflect that change on the user interface because I then also
74
00:05:31,030 --> 00:05:38,410
want to set it back to false once we're done here, so we can set it back to false here right before
75
00:05:38,410 --> 00:05:38,980
we pop
76
00:05:38,980 --> 00:05:44,890
and of course also here inside of then right before we pop. The idea with that is that we can now use
77
00:05:44,890 --> 00:05:49,500
isLoading in our widget tree to render something different whilst we are loading.
78
00:05:49,960 --> 00:05:57,820
So maybe here in the body, instead of rendering the full form, we can check if isLoading is true and
79
00:05:57,820 --> 00:06:05,560
if it is true, then I want to render center widget where the child maybe is a CircularProgressIndicator,
80
00:06:05,770 --> 00:06:11,650
a widget that ships with Flutter and otherwise, I'll render padding with my form. So I'll render this
81
00:06:11,710 --> 00:06:17,500
central loading spinner here in the end with center which comes from Flutter and CircularProgressIndicator
82
00:06:17,500 --> 00:06:22,720
which comes from Flutter from the material package. I render this if we are loading and I only rendered
83
00:06:22,720 --> 00:06:25,060
the other content if we're not loading.
84
00:06:25,090 --> 00:06:38,650
So with that, if we now save this and we add yet another item here, this is another test, if we now add the
85
00:06:38,680 --> 00:06:44,260
image here and save this, you see the loading spinner and we go back once this completed and that's of
86
00:06:44,260 --> 00:06:49,720
course a better user experience because we now let the user know that something is happening for which
87
00:06:49,720 --> 00:06:53,080
we wait to finish until we continue with the app.
88
00:06:53,680 --> 00:06:57,430
So this is a better way of sending our data to a server.
9378
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.