Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,330 --> 00:00:07,990
So the child of my card here should be that row with all the information in it.
2
00:00:08,730 --> 00:00:10,420
So let's add a row here
3
00:00:14,230 --> 00:00:21,390
and in that row, I want to have my bars with the label, with the weekday label and with the amount
4
00:00:21,400 --> 00:00:29,680
and of course with the partially filled background. So we'll have some children in here and we'll always
5
00:00:29,680 --> 00:00:32,170
have exactly seven bars,
6
00:00:32,170 --> 00:00:35,900
that's something I know, nonetheless,
7
00:00:35,940 --> 00:00:44,160
I want to generate these seven bars dynamically and not hardcode seven bars which it's in here.
8
00:00:44,160 --> 00:00:50,960
So therefore, let's add a new method here in our chart widget or a new getter to be precise.
9
00:00:50,970 --> 00:00:56,910
You might remember, getters are these properties which are calculated dynamically and there, I want to
10
00:00:56,910 --> 00:01:03,840
have a getter which I'll name groupedTransactions or transactionValues because these
11
00:01:03,840 --> 00:01:07,330
are the transaction values grouped by weekday
12
00:01:07,590 --> 00:01:13,980
and what this actually returns, what this yields should be a list because I need multiple bars, I need
13
00:01:13,980 --> 00:01:21,030
seven bars and the item, the list item in there, the list items should be maps.
14
00:01:21,030 --> 00:01:24,890
You could also build your own objects, that would be perfectly fine
15
00:01:25,140 --> 00:01:31,290
but I will use a map here and in that map, we'll have mixed content, we'll have keys that are strings
16
00:01:31,290 --> 00:01:35,930
and the values will be of type object because we'll have mixed value there
17
00:01:36,060 --> 00:01:38,700
and it will become clearer what's in there
18
00:01:38,700 --> 00:01:40,820
as we progress here.
19
00:01:41,040 --> 00:01:46,950
So as a getter, this needs a body where we return that list.
20
00:01:46,950 --> 00:01:54,180
Now first of all, we can use list and then generate which is a utility constructor on the list class,
21
00:01:54,390 --> 00:01:58,050
list is a core Dart object or core Dart class
22
00:01:58,080 --> 00:02:05,190
and it has a generate constructor which generates us a new list. We define the length and generate makes
23
00:02:05,190 --> 00:02:10,770
a lot of sense here because I know that I went a length of seven, I want seven weekdays and then we
24
00:02:10,770 --> 00:02:13,830
have a function which will be called for every element.
25
00:02:13,830 --> 00:02:23,280
So here, we pass in a function and that function will receive the index, so 0, 1, all the way up to 6
26
00:02:23,310 --> 00:02:25,820
because the index is always one lower than the length.
27
00:02:26,250 --> 00:02:30,910
So it will give us a list with seven elements and first list element has an index of 0,
28
00:02:31,020 --> 00:02:38,700
so it executes this function for every generated list element with index being 0, 1, 2, 3, 4, 5, 6. In here,
29
00:02:39,060 --> 00:02:48,190
in this function here we pass as a second argument to generate, I return my map in the end right because
30
00:02:48,190 --> 00:02:55,300
I have a list of maps. So we can return something with curly braces to create a map
31
00:02:55,300 --> 00:03:02,330
and now in that map which I return here, I need a couple of pieces of information. I need to get the label
32
00:03:02,330 --> 00:03:07,190
for the weekday and I'll just name this key here day
33
00:03:07,190 --> 00:03:11,290
but it should hold a label like M for Monday, T for Tuesday and so on,
34
00:03:11,330 --> 00:03:14,150
so we'll replace this later, we'll not hardcode this,
35
00:03:14,150 --> 00:03:21,870
no worries. Then I will also need the amount for that day and that amount should be something like 9.99
36
00:03:21,900 --> 00:03:22,330
of course,
37
00:03:22,350 --> 00:03:26,000
so the total spendings for that given day.
38
00:03:26,170 --> 00:03:30,660
Now we need to calculate that amount for that day and for that, we need to know which transactions we
39
00:03:30,660 --> 00:03:32,300
have overall.
40
00:03:32,310 --> 00:03:35,610
So the chart will need our list of transactions
41
00:03:35,610 --> 00:03:44,720
otherwise we can't derive to values for the chart of course. Hence I'll add a final property which should
42
00:03:44,720 --> 00:03:45,590
be a list
43
00:03:48,180 --> 00:03:51,070
full of transactions and transaction is my own class,
44
00:03:51,090 --> 00:03:54,720
so we should import that from the models folder,
45
00:03:54,810 --> 00:04:01,930
the transaction.dart file and I'll name that recent transactions because actually we should only receive
46
00:04:01,930 --> 00:04:02,710
a list of the
47
00:04:02,710 --> 00:04:11,300
recent transactions that happened over the last two days or over the last seven days to be precise.
48
00:04:11,350 --> 00:04:18,610
I'll add a constructor where I expect these recent transactions and store them here in that list.
49
00:04:18,610 --> 00:04:27,870
Now let's continue with the logic for grouping our transactions by weekday. To group these recent transactions
50
00:04:27,870 --> 00:04:32,900
here inside of our generate method or in the function we pass to generate here,
51
00:04:33,090 --> 00:04:39,510
I first of all want to find out which weekday we're currently generating and we can do this with
52
00:04:39,510 --> 00:04:41,400
the help of index.
53
00:04:41,400 --> 00:04:44,820
So I'll create a weekday variable and I mark it as final,
54
00:04:44,820 --> 00:04:50,850
you could use var here but if you don't intend on changing this variable ever, you can also mark it as
55
00:04:50,850 --> 00:04:56,070
final and then this is considered to be better because you're clear about what you're planning and being
56
00:04:56,070 --> 00:04:59,640
clear about your intentions is always good in programming.
57
00:04:59,640 --> 00:05:06,120
So instead of var, I use final to mark that this is a final variable which kind of doesn't really make
58
00:05:06,120 --> 00:05:08,040
it a variable I guess
59
00:05:08,250 --> 00:05:13,110
and then we set this equal to datetime now which is the current date,
60
00:05:13,110 --> 00:05:21,420
so today and now of course we know that since we generate data for the last seven weekdays, when this here
61
00:05:21,720 --> 00:05:30,350
has an index of zero, then we could say we deduct zero days from today and hence we have today
62
00:05:30,430 --> 00:05:34,490
and we can get the weekday of today. For the next item in the list,
63
00:05:34,490 --> 00:05:35,780
this will be one,
64
00:05:35,810 --> 00:05:41,360
so that would be yesterday if we deduct it from datetime now and then we can get the weekday of that
65
00:05:41,590 --> 00:05:45,490
and so on all the way up to one week ago.
66
00:05:45,710 --> 00:05:52,400
Thankfully, the datetime object in Dart has a subtract method where we can conveniently subtract the duration
67
00:05:52,460 --> 00:05:59,750
to get a new date. Duration has to be a duration object and duration is also built into Dart so we can
68
00:05:59,750 --> 00:06:01,410
use it like this
69
00:06:01,730 --> 00:06:07,050
and duration in its constructor takes some arguments that allow us to define the duration
70
00:06:07,070 --> 00:06:12,060
and here we could say the duration should be index days.
71
00:06:12,080 --> 00:06:16,400
So for the first round, when this is zero, we have a duration of zero days,
72
00:06:16,400 --> 00:06:22,310
so we subtract zero days which means we have still datetime now. In the next round, for the next list item
73
00:06:22,310 --> 00:06:24,390
which is generated, index is one,
74
00:06:24,410 --> 00:06:26,150
so we subtract one day,
75
00:06:26,270 --> 00:06:31,720
so that means now, we generate a date which is yesterday and so on.
76
00:06:31,730 --> 00:06:33,680
So with that, we generate these different days
77
00:06:33,680 --> 00:06:44,280
but of course, I'm not really interested in the date, I want to get my weekday shortcut, my identifier
78
00:06:45,090 --> 00:06:49,790
and we can do that with the help of a package we installed earlier with the intl package.
79
00:06:49,860 --> 00:06:57,420
So here you can add the intl package again, the intl.dart file and here when we assign a weekday
80
00:06:57,420 --> 00:07:07,180
value to the day field in our map here, to the day key, we can use DateFormat.E,
81
00:07:07,590 --> 00:07:13,430
that's a special constructor that simply gives us the shortcut for the weekday,
82
00:07:13,440 --> 00:07:15,880
so M for Monday, T for Tuesday and so on,
83
00:07:15,900 --> 00:07:20,570
that's built into that date format class which is of course extremely convenient
84
00:07:20,970 --> 00:07:27,390
and then all we have to do is pass our weekday in here and that will then retrieve the shortcut for
85
00:07:27,390 --> 00:07:33,070
that weekday as a string and store it in this map. So that's the weekday,
86
00:07:33,080 --> 00:07:35,420
what about the total amount for that day?
87
00:07:35,420 --> 00:07:41,490
That of course means that we need to sum up all transactions that happened on this day.
88
00:07:41,540 --> 00:07:45,270
Now we got the recent transactions which are all transactions for the last week,
89
00:07:45,300 --> 00:07:50,070
so now we need to find all transactions that happened on this day and then sum them up.
90
00:07:50,400 --> 00:07:57,900
We can do that with the help of a for loop, so I'll create a variable here, to be precise
91
00:07:57,910 --> 00:08:04,540
I'll create a variable with the double keyword, totalSum and I use double here instead of var because
92
00:08:04,600 --> 00:08:08,350
since I don't initialize this variable, it's unclear which type it will have
93
00:08:08,350 --> 00:08:12,130
and then it's a good practice to clearly define the type it will have
94
00:08:12,130 --> 00:08:13,530
and now we can use a for loop.
95
00:08:13,570 --> 00:08:19,610
We haven't used a for loop before, a for loop allows you to repeat code multiple times.
96
00:08:20,080 --> 00:08:27,950
You do that by typing for and then you pass some instructions here between these parentheses, to be precise
97
00:08:27,950 --> 00:08:35,150
a for loop needs three things; it needs an internal variable which changes with every iteration in the
98
00:08:35,150 --> 00:08:35,770
for loop,
99
00:08:35,810 --> 00:08:44,180
so with every execution of your code and that's created with the var keyword, it's often named i but
100
00:08:44,180 --> 00:08:50,650
you can use any name you want but i for iteration and you assign an initial value, typically that's zero
101
00:08:50,660 --> 00:08:57,560
but you can assign any value, then you add a semicolon and next you define your exit condition,
102
00:08:57,560 --> 00:09:01,160
so how long should this loop keep on running
103
00:09:01,160 --> 00:09:08,250
and here it should keep on running as long as we have transactions in our recent transactions list because
104
00:09:08,250 --> 00:09:14,590
I want to go through all transactions to then filter out the ones that happened today.
105
00:09:14,590 --> 00:09:18,690
So our keep it running condition here, which is a better name than exit,
106
00:09:18,690 --> 00:09:29,960
so our keep it running condition is that i, this index here, is smaller then recent transactions length.
107
00:09:31,040 --> 00:09:33,910
So let's say recent transactions is a list of five elements,
108
00:09:33,920 --> 00:09:38,720
then this loop will keep on running as long as i is smaller than five.
109
00:09:38,730 --> 00:09:42,090
The problem with that is that we never change i,
110
00:09:42,140 --> 00:09:45,110
that's the third element you need in the for loop.
111
00:09:45,110 --> 00:09:48,080
You change i with every iteration,
112
00:09:48,080 --> 00:09:53,750
typically by simply adding one and that's a shortcut for i equals i plus one, which you could also
113
00:09:53,750 --> 00:09:54,390
write,
114
00:09:54,500 --> 00:10:01,100
the shortcut is i++ which means after every round, important, after every round, one gets added
115
00:10:01,250 --> 00:10:03,320
to i. So after the first round,
116
00:10:03,350 --> 00:10:05,860
i is one if you have five elements in here,
117
00:10:05,900 --> 00:10:13,020
it keeps on running and after the last round, i if we have five elements will get a value of five,
118
00:10:13,100 --> 00:10:18,950
that is the not smaller than five anymore and hence it will stop running the loop.
119
00:10:18,950 --> 00:10:23,840
So with that, we're running through all our transactions and inside of the for loop, we can now add an if
120
00:10:23,840 --> 00:10:26,290
statement to check if that transaction
121
00:10:26,450 --> 00:10:32,550
we're now looking at happened today. So that means that now inside the loop in the if statement, I use
122
00:10:32,550 --> 00:10:38,460
recent transactions and access the element at the index i and i will of course change with every iteration.
123
00:10:38,460 --> 00:10:45,660
So we go through all elements in recent transactions and here, I access each element and then I can check
124
00:10:45,810 --> 00:10:55,390
if that element here, if the date of that element is equal to this date.
125
00:10:55,390 --> 00:11:00,880
However this would be wrong because this is a datetime object which means it does not only contain
126
00:11:00,880 --> 00:11:09,380
the date but also the time, the timestamp and this simply means that comparing dates will never succeed
127
00:11:09,440 --> 00:11:12,170
because we'll never have the exact same timestamp,
128
00:11:12,260 --> 00:11:17,780
instead I want to make sure that the day, the month and the year is equal because these are my three
129
00:11:17,810 --> 00:11:19,520
things that make up the date.
130
00:11:19,640 --> 00:11:27,380
So I compare the day and thankfully, there is a day property on every datetime object, I check if day is
131
00:11:27,380 --> 00:11:30,470
equal to and with two equal signs,
132
00:11:30,470 --> 00:11:38,160
as you learned, you can check for value equality, if that is equal to weekday.day.
133
00:11:38,160 --> 00:11:45,180
Now I also need to make sure that we're in the same year and month, so I will also check if recent transactions
134
00:11:45,760 --> 00:11:54,370
for the element i, if there on the date, the month is equal to weekday, month
135
00:11:57,320 --> 00:11:57,920
and
136
00:11:57,930 --> 00:12:02,160
one additional condition which I want to add here and you can split conditions across multiple lines
137
00:12:02,160 --> 00:12:12,880
to keep it simple and I want to check if recent transactions date year is equal to weekday year.
138
00:12:12,900 --> 00:12:15,330
With that, we have a condition that checks
139
00:12:15,390 --> 00:12:19,390
are we looking at a transaction that happened on the weekday,
140
00:12:19,440 --> 00:12:27,870
we're considering for this iteration in our list generation here. If we identified a transaction that
141
00:12:27,870 --> 00:12:29,220
happened on that day,
142
00:12:29,220 --> 00:12:31,670
we want to add it to our total sum
143
00:12:31,740 --> 00:12:37,650
and now actually I want to initialize my total sum with a value, with 0.0 and therefore we can
144
00:12:37,650 --> 00:12:42,980
change double to var because if we initialize it with a value, Dart is able to infer the type.
145
00:12:43,230 --> 00:12:51,240
So in the if check here, I now want to use total sum and add, with the shortcut here,
146
00:12:51,240 --> 00:12:57,450
my recent transaction for the given index amount because I need to add that amount to the total sum
147
00:12:57,480 --> 00:13:01,480
and with that, we go through all transactions and we add the sum of each transaction,
148
00:13:01,500 --> 00:13:07,380
the amount of each transaction that happened on that weekday we're looking at to the total sum for that
149
00:13:07,410 --> 00:13:13,540
weekday and now down there when we generate the map, we just have to replace 9.99 with the total sum
150
00:13:13,540 --> 00:13:19,890
and now we should be returning a map with the weekday shortcut and the amount. That was a lot of work
151
00:13:19,930 --> 00:13:25,770
but I hope the logic here is clear which allows us to find out which amount of transactions, which total
152
00:13:25,770 --> 00:13:33,600
sum was generated on a given weekday. To see whether that worked, I'll actually print these two pieces
153
00:13:33,600 --> 00:13:35,160
of information here,
154
00:13:35,190 --> 00:13:41,580
so here, right before we return the map, I'll print the weekday identifier and the total sum
155
00:13:41,580 --> 00:13:45,150
but now of course to see that, we also need to wire up our chart widget.
17503
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.