Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,360 --> 00:00:07,670
So string interpolation is a really useful feature and something I will use throughout this course and which
2
00:00:07,670 --> 00:00:11,710
I would recommend using because it can save you some extra code.
3
00:00:11,720 --> 00:00:17,450
Now let's also format the date however and there, instead of just printing it as a string like this
4
00:00:17,450 --> 00:00:23,540
which is relatively ugly, it's certainly human readable but it's not really that beautiful,
5
00:00:23,570 --> 00:00:31,250
instead of using that, I'd be more interested in actually having a nicely formatted date and that can
6
00:00:31,250 --> 00:00:34,320
easily be achieved with the help of an extra package and
7
00:00:34,340 --> 00:00:35,690
that's correct.
8
00:00:35,690 --> 00:00:41,780
There is no built-in feature, neither in Dart nor in Flutter that would make it easy for us to format
9
00:00:41,810 --> 00:00:42,930
this date.
10
00:00:42,950 --> 00:00:45,900
Now obviously, we could always try to do it on our own,
11
00:00:45,920 --> 00:00:52,130
we could write our own code that takes this string and for example only extracts the date part here
12
00:00:52,130 --> 00:00:57,590
at the beginning but writing that code on our own would be pretty cumbersome and thankfully, there is
13
00:00:57,590 --> 00:01:04,330
a package, a third-party package which we can use. If you search for a Dart date format,
14
00:01:04,330 --> 00:01:07,300
you will find this intl package here.
15
00:01:07,450 --> 00:01:14,950
Click on it and you should be taken here to a description of this package here on pub.dev. Now
16
00:01:14,950 --> 00:01:19,310
pub.dev is an important page in the Dart and Flutter universe,
17
00:01:19,520 --> 00:01:26,980
it's a site, a webpage, where you find a lot of packages you can install into your Dart projects and
18
00:01:26,980 --> 00:01:30,310
we will actually use a lot of these packages throughout the course,
19
00:01:30,310 --> 00:01:36,050
for example later also for things like HTTP requests or using Google maps.
20
00:01:36,070 --> 00:01:42,400
So here on pub.dev, we're now on the page of the intl package and intl stands for internationalisation
21
00:01:42,700 --> 00:01:47,560
because this actually is a package that helps us with localizing our app,
22
00:01:47,560 --> 00:01:55,020
we could for example transform strings here to different text for the different locales
23
00:01:55,030 --> 00:01:59,980
we might be targeting, so the different languages we might be targeting but for a start, we can also
24
00:01:59,980 --> 00:02:06,460
use this package to simply transform a date to the English locale but there, to a human readable string
25
00:02:06,460 --> 00:02:10,970
that is nicer than this string we have here.
26
00:02:11,050 --> 00:02:15,790
So for this, we need to install this package and clicking on installing here, you get instructions on
27
00:02:15,790 --> 00:02:16,780
how to add it.
28
00:02:16,810 --> 00:02:18,100
It's super straightforward,
29
00:02:18,100 --> 00:02:24,130
you can just copy that part here, so the package name and the version number and then go to your project
30
00:02:24,130 --> 00:02:29,410
and there to the pubspec.yaml file because the pubspec.yaml file is the file which
31
00:02:29,410 --> 00:02:35,680
in the end manages the the setup of your project and most importantly, the dependencies of your project. So
32
00:02:35,680 --> 00:02:42,520
which other packages can we use in this Dart project here? There, we already have Flutter as a dependency
33
00:02:42,850 --> 00:02:49,420
and below that, on the same level as Flutter, we can add intl and the version number, so what you just
34
00:02:49,420 --> 00:02:50,350
copied.
35
00:02:50,350 --> 00:02:57,670
Now one important note, in .yaml files, indentation is very important, indentation expresses how things
36
00:02:57,730 --> 00:03:03,040
are related. So it's important that you don't add intl here on the same level as dependencies or on
37
00:03:03,040 --> 00:03:06,990
the same level as SDK or one space too far on the right.
38
00:03:07,270 --> 00:03:12,750
So you can hit space twice and you're on the same level as Flutter and it has to be on that level.
39
00:03:12,790 --> 00:03:18,670
Now if you save this file, Flutter should automatically go ahead, thanks to the Flutter and Dart
40
00:03:18,670 --> 00:03:24,430
extensions which you have installed and install that package on your machine and enable it in this
41
00:03:24,430 --> 00:03:29,680
project. Important, the source code for this package will not be stored in this project,
42
00:03:29,680 --> 00:03:35,380
it's stored somewhere else, typically in your user folder but a connection is established in your project
43
00:03:35,500 --> 00:03:37,920
so that you can use this package
44
00:03:38,140 --> 00:03:43,080
and now we can use it here in the main.dart file to output our date here
45
00:03:43,270 --> 00:03:51,670
as more human readable text. For this, let's go back to the package page, you can check out the API reference
46
00:03:51,670 --> 00:03:57,490
here on the right to find the documentation for this package and here, you learned how to use it and
47
00:03:57,640 --> 00:04:04,630
you can configure a lot there because as I explained, you could use this package for actually translating
48
00:04:04,690 --> 00:04:06,560
your app or parts of your app,
49
00:04:06,640 --> 00:04:12,790
things like dates and numbers to different locales but here you'll also find the basic syntax for formatting
50
00:04:12,850 --> 00:04:15,790
a date and in the end, that is what we will use here.
51
00:04:16,300 --> 00:04:21,550
So in the main.dart file where I want to format the date, I first of all have to import this package,
52
00:04:21,550 --> 00:04:27,220
you always have to import something to use its features. So the package name here now is intl and
53
00:04:27,220 --> 00:04:33,420
there you want to import intl.dart. With that import added in the main.dart file,
54
00:04:33,420 --> 00:04:42,420
we can go down there where we have our date string and now here, we can use date format and build an
55
00:04:42,420 --> 00:04:45,350
object with the constructor here.
56
00:04:45,360 --> 00:04:49,230
This is a class that is now made available by the intl package
57
00:04:49,320 --> 00:04:54,390
and on that date format object, you can call format and pass a date into this,
58
00:04:54,390 --> 00:04:58,960
so now not a string but a date. Conveniently tx.date gives us a date,
59
00:04:58,980 --> 00:05:04,790
so let's just get rid of to string and add this and format will then automatically return the string,
60
00:05:06,190 --> 00:05:07,900
that's exactly what we need here.
61
00:05:07,960 --> 00:05:12,940
With that if you save it and you go back to your application, now you see this looks way better,
62
00:05:12,940 --> 00:05:18,700
we get this beautiful string here out of the box, thanks to this format package or to this intl package
63
00:05:18,700 --> 00:05:20,680
and the format method there.
64
00:05:20,830 --> 00:05:28,480
The thing just is I only want the date and you can actually define the pattern of the string that should
65
00:05:28,480 --> 00:05:29,750
be generated.
66
00:05:29,770 --> 00:05:30,990
There are two ways of doing that,
67
00:05:31,000 --> 00:05:32,850
for one you can pass a pattern string
68
00:05:32,850 --> 00:05:38,800
here to date format and now this has its own language for defining these patterns and you can learn
69
00:05:38,800 --> 00:05:43,720
how to define these patterns in the official docs of the intl package which I just showed you a second
70
00:05:43,720 --> 00:05:44,640
ago.
71
00:05:44,740 --> 00:05:52,150
There you can for example enter things like yyyy to indicate that you want to first print the year
72
00:05:52,150 --> 00:05:55,330
and there, you want to print it by including all four digits,
73
00:05:55,450 --> 00:06:00,940
then maybe a dash, then maybe the month and then the day
74
00:06:00,940 --> 00:06:03,970
and there is a difference between capital and lowercase characters
75
00:06:03,970 --> 00:06:09,550
and again the official docs of this intl package and there, the date format functionality
76
00:06:09,640 --> 00:06:15,940
show you all the different patterns you can use and different meanings of the different characters.
77
00:06:15,940 --> 00:06:21,010
So if you would use that here, we for example get a date that is formatted like this,
78
00:06:21,010 --> 00:06:26,230
we have the dash between the year and the month and the day because I added the dash here. We could of
79
00:06:26,230 --> 00:06:28,260
course also use a slash instead,
80
00:06:28,270 --> 00:06:29,650
only the characters here,
81
00:06:29,650 --> 00:06:35,260
so y, m and d, these are special values which are interpreted by the date format,
82
00:06:35,380 --> 00:06:38,350
what you put between these characters is basically up to you,
83
00:06:38,350 --> 00:06:41,690
so now we would have a slash in between.
84
00:06:41,780 --> 00:06:42,930
Well that's one thing,
85
00:06:42,930 --> 00:06:49,410
there also are a lot of preconfigured configurations and these are all made available as special constructors.
86
00:06:49,740 --> 00:06:55,640
So you can also add a dot here after date format to access these special constructors
87
00:06:55,710 --> 00:07:02,880
and there you have tons of constructors as you can tell. Constructors for all kinds of stuff, like for
88
00:07:02,880 --> 00:07:08,970
example here for a year, then a month in a written out form and then a day
89
00:07:08,970 --> 00:07:14,850
and if I use this here, I don't have to add a pattern because this preconfigures a pattern and now
90
00:07:14,970 --> 00:07:18,370
we have this here as a date. And I will go with that,
91
00:07:18,390 --> 00:07:23,850
however feel free to play around with that, choose different patterns, different formats dive into the official
92
00:07:23,850 --> 00:07:27,210
docs and fine tune the date to your requirements and preferences.
10499
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.