Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,470 --> 00:00:11,190
Let's use our chart bar. Back in a chart widget here where I currently return some text, I of course instead
2
00:00:11,190 --> 00:00:14,950
want to return my chart bar widget. For that
3
00:00:14,970 --> 00:00:19,420
as always, you need to import it by pointing at the
4
00:00:22,350 --> 00:00:26,660
chart_bar.dart file. With that imported here,
5
00:00:26,660 --> 00:00:32,870
I return my chart bar and now we need to pass in three things - the label, which is not hard, in our data
6
00:00:32,870 --> 00:00:33,610
object here,
7
00:00:33,620 --> 00:00:38,200
we're referring of course to this map here and there,
8
00:00:38,230 --> 00:00:44,120
the day is our label, should be wrapped in quotes.
9
00:00:44,140 --> 00:00:46,370
The second argument also is not hard,
10
00:00:46,420 --> 00:00:55,980
it's our spending amount and that's data and then amount but the third part is more difficult,
11
00:00:55,980 --> 00:01:05,180
we need the total spendings for that week. Let's calculate those with another getter which will return
12
00:01:05,180 --> 00:01:08,280
a double and I'll name it Max
13
00:01:08,310 --> 00:01:16,680
spending and here, I want to return a value based on my grouped transaction values because there in
14
00:01:16,680 --> 00:01:22,180
end, we want to sum up all the amounts, the sum of all the total sums for each day gives us the total
15
00:01:22,180 --> 00:01:27,860
sum for the entire week. Now grouped transaction values of course is a list and now here, I just need
16
00:01:27,860 --> 00:01:29,260
a double.
17
00:01:29,310 --> 00:01:35,370
There is another helpful method we can use in Dart on lists and that's the fold method. Fold allows
18
00:01:35,370 --> 00:01:42,090
us to change a list to another type, with a certain logic we define in the function we pass to fold.
19
00:01:43,120 --> 00:01:48,940
So here to fold, we pass an anonymous function and besides that as a first argument, we pass a starting
20
00:01:48,940 --> 00:01:49,450
value, in
21
00:01:49,450 --> 00:01:55,480
this case, that's 0.0. So that's our starting value and now in this function, our second argument we pass to
22
00:01:55,480 --> 00:01:56,290
fold,
23
00:01:56,290 --> 00:02:00,730
we have to return a new value which will be added to this starting value,
24
00:02:00,850 --> 00:02:05,210
we have to return a new value now for every run on every item in groups.
25
00:02:05,200 --> 00:02:06,520
transaction values.
26
00:02:06,740 --> 00:02:08,800
Therefore in is anonymous function,
27
00:02:08,970 --> 00:02:15,780
we received two arguments - the first argument is the currently calculated sum,
28
00:02:15,790 --> 00:02:22,780
so our starting value for the first element and thereafter, the current sum or the current value we calculated
29
00:02:22,810 --> 00:02:24,010
based on this function,
30
00:02:24,700 --> 00:02:26,520
so our sum
31
00:02:26,920 --> 00:02:29,770
and the second argument we get here is the element we're looking at,
32
00:02:29,770 --> 00:02:35,830
so our item or whatever you want to call it. These names are up to you because you get the values passed
33
00:02:35,830 --> 00:02:41,000
by Dart but you are writing this code here which is triggered by Dart,
34
00:02:41,040 --> 00:02:43,590
so you can name these things here as you want.
35
00:02:43,650 --> 00:02:48,570
Now in here, you have to return a new sum which will then be passed as an input to this function for the
36
00:02:48,570 --> 00:02:49,860
next element in line
37
00:02:49,950 --> 00:02:55,500
in this list. If you're having a Javascript background, this is basically your equivalent to the reduce
38
00:02:55,500 --> 00:02:56,170
function,
39
00:02:56,210 --> 00:02:59,200
this also exists in many other programming languages.
40
00:02:59,260 --> 00:03:06,120
Now in here, I now simply return sum plus item amount and this should give us all the totals for each
41
00:03:06,120 --> 00:03:12,200
day and therefore sum up the total for the week and therefore I'll name this total spending here. So now
42
00:03:12,200 --> 00:03:15,320
with that, we have a getter for the total spending.
43
00:03:15,330 --> 00:03:20,490
Now here we could pass the total spending in but actually in the chart bar, I'm not interested in my
44
00:03:20,490 --> 00:03:23,850
total spending but in the percentage of the total.
45
00:03:23,850 --> 00:03:29,900
So actually what we should do here before passing this in, we have to take the amount for any given weekday
46
00:03:29,910 --> 00:03:37,140
we're looking at and we have to divide this by our total spending, so that we get the ratio of how much
47
00:03:37,320 --> 00:03:40,900
of our total week spending did we spend on this day.
48
00:03:40,920 --> 00:03:46,660
Now here we are getting an error that we can't use that operator on the type object because Dart is
49
00:03:46,660 --> 00:03:52,360
not able to find out that the amount is a double and we can fix this by wrapping this in parentheses and
50
00:03:52,360 --> 00:04:01,120
using as double here. And now with that, we should have everything to draw our chart with some bars onto
51
00:04:01,130 --> 00:04:04,610
the screen and that's looking good.
52
00:04:04,610 --> 00:04:06,830
Of course we're not using the space very efficiently,
53
00:04:06,830 --> 00:04:10,850
that's something we should work on but other than that, I'm liking this.
54
00:04:10,850 --> 00:04:20,930
If I add a new transaction now, more shoes for let's say $120.99, you see this updates,
55
00:04:20,930 --> 00:04:27,290
this number updates and always the the first bar is of course fully occupied because we have no
56
00:04:27,290 --> 00:04:29,140
way of targeting different days.
57
00:04:29,420 --> 00:04:35,870
Now we actually do get an error though, if we restart the app and therefore we lose all our transactions
58
00:04:35,930 --> 00:04:40,480
which are stored in memory of our application because right now, we're not saving them anywhere, not on
59
00:04:40,480 --> 00:04:42,390
a server or anything like that
60
00:04:42,500 --> 00:04:47,160
and of course later in the course, you will learn how to save data on a server and load it from there
61
00:04:47,270 --> 00:04:51,660
but right now, our data is lost whenever we restart the app and for this app, this is fine,
62
00:04:51,660 --> 00:04:55,850
it's just our next step in our journey to master Flutter.
63
00:04:55,850 --> 00:05:00,980
The problem nonetheless is that if we have no transactions, our chart here breaks and we get an error.
64
00:05:01,610 --> 00:05:08,780
Reason for that of course is that here, the fraction we're passing to our chart bar, which defines
65
00:05:08,870 --> 00:05:10,550
how much it should be filled,
66
00:05:10,580 --> 00:05:16,480
that of course is an invalid value if we have no transactions because then total spending is zero,
67
00:05:16,490 --> 00:05:21,130
we haven't spent anything, we have no transactions and dividing by zero,
68
00:05:21,290 --> 00:05:25,310
never a great idea. This actually yields an invalid value
69
00:05:25,310 --> 00:05:31,220
and what I actually would want to do is of course check whether total spending is zero and if it is
70
00:05:31,220 --> 00:05:31,570
zero,
71
00:05:31,580 --> 00:05:38,000
if we have no total spending, then I want to pass zero to all my chart bars because then each chart bar
72
00:05:38,000 --> 00:05:42,550
should be empty and therefore the fraction by which it should be filled should be zero.
73
00:05:42,560 --> 00:05:46,880
So in the end, we can simply add a ternary expression here and check whether total spending is equal
74
00:05:46,880 --> 00:05:48,230
to 0.0
75
00:05:48,230 --> 00:05:55,250
and if that's the case, pass 0.0 to the chart bar as our percentage of total because the bar should be
76
00:05:55,250 --> 00:06:02,180
empty then and only if we have a non-zero value, I want to calculate my share of the total spending.
77
00:06:02,180 --> 00:06:08,000
So with this simple change here, now this renders again and now we have an application that also works with
78
00:06:08,000 --> 00:06:12,880
no transactions. So next steps will of course be that we target different days
79
00:06:12,890 --> 00:06:16,760
but first of all, I want to make sure that we use this space a bit more efficiently.
8694
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.