Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,850 --> 00:00:07,950
To wire up the chart widget, we need to include it in the place where we want to display it
2
00:00:07,950 --> 00:00:12,660
and that would be in the main.dart file instead of our chart placeholder.
3
00:00:12,660 --> 00:00:14,700
So here, we have that chart placeholder,
4
00:00:14,700 --> 00:00:20,660
let's replace that entire container now with our chart widget. The chart widget is only available
5
00:00:20,660 --> 00:00:21,440
if we import it,
6
00:00:21,470 --> 00:00:25,550
so here we need to import ./widgets/chart
7
00:00:25,580 --> 00:00:32,180
and let me also bring that import here next to the other widgets imports, not required but good from
8
00:00:32,180 --> 00:00:34,130
a stylistic perspective.
9
00:00:34,130 --> 00:00:37,730
So we're importing the chart widget, we're using it down there,
10
00:00:37,820 --> 00:00:41,880
chart however needs our recent transactions and that's important,
11
00:00:41,930 --> 00:00:44,600
it does not need all transactions.
12
00:00:44,600 --> 00:00:49,580
We could have more transactions here but I only want to transactions from the last week,
13
00:00:49,580 --> 00:00:53,950
so we should make sure that we only add those. For that in the main.dart file,
14
00:00:53,960 --> 00:00:55,450
I will also add a getter.
15
00:00:55,580 --> 00:00:58,760
I have my list of transactions here and below that,
16
00:00:58,760 --> 00:01:05,810
I'll add another list of transactions but this is now a getter, so a dynamically calculated property
17
00:01:06,320 --> 00:01:10,090
which I'll name _recentTransactions.
18
00:01:10,220 --> 00:01:15,440
Now just as all getters, this has no argument lists but it has a body and it needs to return here
19
00:01:15,530 --> 00:01:20,910
a list of transactions and for that, we of course take all our transactions
20
00:01:20,910 --> 00:01:26,240
but now we want to filter out transactions that didn't happen in the last week.
21
00:01:26,550 --> 00:01:28,890
We could again do this with the help of a for loop,
22
00:01:28,920 --> 00:01:35,310
we could build a new list in here and return that new list after running over all transactions with
23
00:01:35,310 --> 00:01:43,690
a for loop and finding all transactions that have a date that is younger than today minus 7 days but
24
00:01:43,690 --> 00:01:48,510
to mix things up and to show an alternative to using for loops everywhere,
25
00:01:48,580 --> 00:01:53,260
you can also use another method which exists on every list which is a default method
26
00:01:53,260 --> 00:01:59,860
Dart offers on lists and that's the where method. Where allows you to run a function on every item in
27
00:01:59,860 --> 00:02:00,510
a list
28
00:02:00,550 --> 00:02:05,590
and if that function returns true, the item is kept in a newly returned list,
29
00:02:05,680 --> 00:02:10,140
otherwise it's not included in that newly returned list.
30
00:02:10,150 --> 00:02:17,590
So here, I pass in an anonymous function and that function gets the element of our list because it executes
31
00:02:17,620 --> 00:02:22,870
on every element in this list and therefore it gets the element it's currently looking at as an input
32
00:02:22,900 --> 00:02:25,470
and here I get my transaction which I'll name tx,
33
00:02:25,480 --> 00:02:29,000
you can name it whatever you want and I want to return true
34
00:02:29,290 --> 00:02:34,180
if that happened within the last week and false if it's older than a week. And for this, I'll check if
35
00:02:34,180 --> 00:02:41,860
tx date and the good thing is that's a datetime object and there, Dart gives us an isAfter method and
36
00:02:41,860 --> 00:02:44,350
now all we have to pass here is another datetime
37
00:02:44,410 --> 00:02:50,290
and only if this date is after this date, this here will return true and only then this transaction
38
00:02:50,500 --> 00:02:57,490
will be included in our recent transactions. So the date I want to pass here is of course today minus
39
00:02:57,550 --> 00:02:58,960
7 days.
40
00:02:58,960 --> 00:03:07,780
So here, I create datetime now and I subtract a duration, you already saw all of that earlier, a duration
41
00:03:07,840 --> 00:03:16,840
where days is equal to 7 and this subtracts 7 days from today and hence makes sure that we check that
42
00:03:16,840 --> 00:03:24,090
this date here is after today minus 7 days. Only transactions that are younger than 7 days are included
43
00:03:24,090 --> 00:03:32,130
here and now we can pass recent transactions, like a property because it is a property, just a dynamically
44
00:03:32,130 --> 00:03:43,280
generated one, to chart. So let's save this and we get an error, the error here is if you scroll up, let
45
00:03:43,430 --> 00:03:49,660
me increase that, if you scroll up, you see that the error is that it has something to do with an iterable which means
46
00:03:49,720 --> 00:03:56,710
something with a list and it's not of subtype list transaction instead it's a where iterable transaction.
47
00:03:57,500 --> 00:04:03,670
A bit cryptic but it has to do with with where and iterables and since it appeared after writing this code
48
00:04:03,670 --> 00:04:07,910
for the recent transactions, it probably has something to do with that.
49
00:04:08,320 --> 00:04:14,360
Indeed, where returns an iterable, not a list but we expect a list here.
50
00:04:14,440 --> 00:04:20,260
So what we need to do here is on the value we generate here with where, we need to call to list and you
51
00:04:20,260 --> 00:04:23,440
might remember we had to do this with map as well earlier.
52
00:04:24,100 --> 00:04:37,100
So now if we save this, now our app restarts and now let's add a transaction here, new shoes 99.99
53
00:04:37,370 --> 00:04:40,700
and if we save that,
54
00:04:41,070 --> 00:04:43,140
nothing happens in our console,
55
00:04:43,140 --> 00:04:45,720
so is our chart logic not working here for the grouped
56
00:04:45,720 --> 00:04:47,560
transaction values?
57
00:04:47,640 --> 00:04:49,820
Well we can't tell because we're simply not using that.
58
00:04:49,920 --> 00:04:53,700
It's a getter and we're never using the grouped transaction values.
59
00:04:53,700 --> 00:04:56,670
So in order to find out whether that works, we need to use it
60
00:04:56,670 --> 00:05:07,760
and for a start, I will simply print grouped transaction values here in the build method of my chart.
61
00:05:07,850 --> 00:05:10,430
And with that, it crashes,
62
00:05:10,430 --> 00:05:12,050
let's see what's wrong here,
63
00:05:14,130 --> 00:05:22,630
the problem is type datetime time is not a subtype of type string, caused by line 26 in our chart file.
64
00:05:22,630 --> 00:05:25,760
Yes, the problem is little mistake on my side,
65
00:05:25,810 --> 00:05:32,290
we shouldn't pass weekday here to this E constructor but instead call format on the object that constructor
66
00:05:32,320 --> 00:05:35,880
gives us and pass weekday to that format function,
67
00:05:35,890 --> 00:05:38,900
also down there of course. So with that
68
00:05:38,910 --> 00:05:40,620
let's now save this
69
00:05:41,070 --> 00:05:47,280
and if we now scroll down, that looks better and indeed what we see here is a couple of things.
70
00:05:47,310 --> 00:05:53,070
This is the overall list that was generated and this looks good, it has a list of days with the different
71
00:05:53,070 --> 00:06:01,610
weekdays and we see 99.99 which is the value I added today on Thursday and
72
00:06:01,620 --> 00:06:04,160
today indeed is Thursday.
73
00:06:04,160 --> 00:06:11,240
So this is working and it's generating this correctly as it seems, let's validate it by adding one other transaction,
74
00:06:12,340 --> 00:06:13,270
groceries
75
00:06:15,750 --> 00:06:21,840
9.69, this will also be added today because right now we have no way of targeting different
76
00:06:21,840 --> 00:06:23,100
days
77
00:06:23,280 --> 00:06:24,980
and with that,
78
00:06:25,020 --> 00:06:25,670
yes,
79
00:06:25,680 --> 00:06:28,500
our amount today increased. That strange number here by the way,
80
00:06:28,500 --> 00:06:29,970
why it's looking that strange,
81
00:06:30,060 --> 00:06:31,320
this is not a bug,
82
00:06:31,320 --> 00:06:33,740
this is a default Dart behavior,
83
00:06:33,750 --> 00:06:39,360
you actually have that behavior in every programming language. When you do calculations with numbers
84
00:06:39,390 --> 00:06:40,940
with floating point values,
85
00:06:40,950 --> 00:06:48,210
so with decimal places, then that's hard for computers and you will therefore, when adding various decimal
86
00:06:48,210 --> 00:06:56,040
places, sometimes end up with long expressions like this, with a lot of decimal places.
87
00:06:56,040 --> 00:07:00,420
Doesn't really matter for us here however, it's not a bug, it's a default behavior which you have in any
88
00:07:00,420 --> 00:07:05,610
programming language, more information about that can be found attached in a document to this lecture
89
00:07:05,610 --> 00:07:11,610
if you want to learn more. For the moment, let's leave that as it is and let's make sure that we now also
90
00:07:11,610 --> 00:07:18,900
don't just see our data here in the debug log with the help of these print statements but that we instead
91
00:07:18,900 --> 00:07:22,100
really output something on the screen for our transactions.
9785
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.