Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,350 --> 00:00:08,500
With this first output on the screen, before we dig deeper into this app and build something more beautiful,
2
00:00:09,040 --> 00:00:14,710
let me take a step back and go back to Dart because it's so important that you also understand Dart
3
00:00:14,890 --> 00:00:16,880
whilst we are working on this.
4
00:00:17,260 --> 00:00:20,250
For one, let me go back to MaterialApp and text.
5
00:00:20,350 --> 00:00:28,330
I mentioned that these are not functions but classes and yet, I pass some data to them right,
6
00:00:28,330 --> 00:00:35,770
I passed the hello string to text and I even have this more strange looking home colon thing here for
7
00:00:35,770 --> 00:00:37,560
MaterialApp.
8
00:00:37,600 --> 00:00:43,030
Let's go back to our little Dart example here on DartPad which we started working on earlier.
9
00:00:43,270 --> 00:00:47,380
There we also have person and person has some default values here.
10
00:00:47,380 --> 00:00:53,800
Now we can override these defaults but what if our class should be a bit more generic, for example for
11
00:00:53,800 --> 00:00:57,950
a person it doesn't really make sense that the default would be Max and 30,
12
00:00:58,120 --> 00:01:04,600
if we're for example building an application where users can sign up, then why would we assume that the
13
00:01:04,690 --> 00:01:06,550
average user is named Max,
14
00:01:06,580 --> 00:01:08,810
that does not make any sense.
15
00:01:08,860 --> 00:01:16,180
So you might want a setup where you have no initial values but instead when this class gets created,
16
00:01:16,180 --> 00:01:20,170
we can pass the initial values here between the parentheses
17
00:01:20,290 --> 00:01:28,720
so that I have Max and 30 here and I have Manu and 31 here and then I don't need to override
18
00:01:28,720 --> 00:01:30,070
my name down there.
19
00:01:30,190 --> 00:01:33,040
That would be nice but right now, it's not supported,
20
00:01:33,280 --> 00:01:40,780
we can support it by adding a so-called constructor to this class. A constructor is a function inside
21
00:01:40,780 --> 00:01:46,720
of a class and therefore we could also call it a method because functions inside of classes are methods,
22
00:01:47,050 --> 00:01:48,860
it's just a different name,
23
00:01:48,970 --> 00:01:56,410
it's a function that of a class which is different to other functions inside of the class in that it always
24
00:01:56,410 --> 00:02:03,340
executes only once when we create a new object based on a class which happens when we call a class
25
00:02:03,340 --> 00:02:04,550
like this.
26
00:02:04,750 --> 00:02:11,110
You add a constructor by repeating the name of the class, in this case person like that. In other programming
27
00:02:11,110 --> 00:02:11,580
languages,
28
00:02:11,590 --> 00:02:17,110
you would write constructor for example but in Dart, it's really just the name repeated.
29
00:02:17,110 --> 00:02:22,210
You add parentheses, like for a normal function and then you add curly braces which holds the code that
30
00:02:22,210 --> 00:02:22,830
should run
31
00:02:22,930 --> 00:02:25,140
when this class gets created.
32
00:02:25,220 --> 00:02:35,560
Now here, you can accept arguments like name and like age or input name and input age to avoid confusion
33
00:02:36,550 --> 00:02:42,520
with the names we chose up there, though you could use the same names because Dart also has a feature
34
00:02:42,520 --> 00:02:49,750
called scoping, which means that if you had the same names as up here, here in the arguments, it would
35
00:02:49,750 --> 00:02:52,300
not override this or anything like that,
36
00:02:52,350 --> 00:02:57,270
instead it would simply just create new variables inside of person,
37
00:02:57,280 --> 00:03:01,930
it always creates new variables for every argument you get and these variables could have the same name
38
00:03:02,110 --> 00:03:07,660
and they would not clash because Dart would behind the scenes manage that separation between your
39
00:03:07,660 --> 00:03:12,400
class level variables and your function or constructor level variables.
40
00:03:12,490 --> 00:03:14,140
But here, to not confuse you,
41
00:03:14,170 --> 00:03:20,860
I chose different names and now inside of the constructor body, we could refer to our name up
42
00:03:20,860 --> 00:03:28,090
here by just typing name and assigning input name as a value and the same for the age.
43
00:03:28,090 --> 00:03:30,420
Now by the way, if you had the same name,
44
00:03:30,460 --> 00:03:37,570
so let's say I had age here, then of course Dart would not be able to figure out if we mean the age up
45
00:03:37,570 --> 00:03:43,660
here or the age here when we're using age in the body of the constructor and therefore, it gives us a
46
00:03:43,660 --> 00:03:47,740
special this keyword which refers to the class itself
47
00:03:47,830 --> 00:03:54,250
and with this .age, we can tell Dart that here, I want to refer to the class level age variable whereas
48
00:03:54,250 --> 00:03:59,560
without this, I want to refer to the function or constructor level variable.
49
00:03:59,560 --> 00:04:03,610
So this is the code we can use, we could do the same for name, just want to show the two different options
50
00:04:04,030 --> 00:04:10,930
and now with that, we have a constructor which allows us to create our object if I run this with different
51
00:04:11,050 --> 00:04:16,860
default values down there and which is of course pretty convenient and a feature you'll use quite a lot.
52
00:04:16,990 --> 00:04:22,480
That's also the feature text uses here which accepts a string which you can pass to the constructor
53
00:04:22,630 --> 00:04:28,330
which you will use to output something because having a default text in the text class would not make
54
00:04:28,330 --> 00:04:29,510
that much sense.
55
00:04:30,930 --> 00:04:40,140
Now besides using this syntax here, you can also use named arguments and that means that you simply wrap
56
00:04:40,140 --> 00:04:43,700
that here in curly braces.
57
00:04:43,710 --> 00:04:50,430
Now what this does is, it means that now these arguments are all optional and therefore of course you
58
00:04:50,430 --> 00:04:59,280
should write code that can live with no values being provided and that you do target them by name
59
00:04:59,310 --> 00:05:00,850
when you're creating your data.
60
00:05:00,870 --> 00:05:06,630
So here you would then target an argument by name, by repeating its name here when you create an instance
61
00:05:06,630 --> 00:05:16,580
based on that object, like this, inputName: and also age:.
62
00:05:16,710 --> 00:05:23,610
That concept of named arguments is also not only available for constructors but also for normal functions.
63
00:05:24,210 --> 00:05:25,190
Now if I repeat this,
64
00:05:25,200 --> 00:05:26,340
it still works.
65
00:05:26,340 --> 00:05:28,570
Now why would I use named arguments?
66
00:05:28,590 --> 00:05:35,550
It's especially great for constructors or functions that take a lot of arguments because if you have normal
67
00:05:35,700 --> 00:05:43,110
positional, so non-named arguments, you have to remember which argument goes into which position, that
68
00:05:43,110 --> 00:05:44,660
the name is the first and the age is
69
00:05:44,670 --> 00:05:49,260
the second argument. Now obviously for a constructor with two arguments, that is doable but if you had
70
00:05:49,290 --> 00:05:53,090
10 arguments, it would be more difficult. By using named arguments,
71
00:05:53,100 --> 00:05:57,660
you can simply use the name to assign it and you can also mix and match the order here if you wanted
72
00:05:57,660 --> 00:05:58,170
to.
73
00:05:58,170 --> 00:06:03,390
So here I could first assign the age and then name because I'm targeting it with the name, right,
74
00:06:03,390 --> 00:06:08,550
so with the name of the argument and therefore, the order doesn't matter, here for positional arguments
75
00:06:08,880 --> 00:06:15,460
as the name suggests, it does matter. Now since I mentioned that all named arguments automatically are
76
00:06:15,460 --> 00:06:16,740
also optional,
77
00:06:16,960 --> 00:06:24,400
you also should either define default values here with an equal sign, right in here in the argument list,
78
00:06:24,400 --> 00:06:27,880
that's something you can do. Now in case you don't provide an age,
79
00:06:28,020 --> 00:06:35,560
the default value would be used and that can of course be useful to avoid that your code breaks or
80
00:06:35,650 --> 00:06:43,630
you add a special annotation, the @required annotation which will ensure that the compiler yells at
81
00:06:43,630 --> 00:06:48,570
you if you don't provide a value for that named argument.
82
00:06:48,580 --> 00:06:54,430
So with that, you turn that optional named argument back into a required one.
83
00:06:54,430 --> 00:07:00,250
Now side note, required is not a feature built into Dart and therefore here, I get an error that Dart
84
00:07:00,280 --> 00:07:04,510
doesn't understand what @required means but it will be built into Flutter,
85
00:07:04,540 --> 00:07:08,670
so here we will be able to use that and with that, it's hopefully a bit clearer
86
00:07:08,710 --> 00:07:13,000
what named arguments are and what constructors are. Now
87
00:07:13,150 --> 00:07:14,570
one last side note,
88
00:07:14,710 --> 00:07:21,220
if I remove @required here, one last side note because it is important, let me add the default value
89
00:07:21,220 --> 00:07:24,710
again. Sometimes,
90
00:07:24,890 --> 00:07:31,460
this set up here where you get certain data and you want to assign it to variables of your class is
91
00:07:31,460 --> 00:07:35,390
so common that you don't have to write all that code,
92
00:07:35,390 --> 00:07:41,480
instead you can get rid of your constructor body, add a semicolon after it and now just make sure that
93
00:07:41,480 --> 00:07:47,330
you use your variable or your property names up here, so that you use name and age
94
00:07:47,600 --> 00:07:48,200
and now here,
95
00:07:52,310 --> 00:07:57,890
you can simply target this age and here also, this name, now without a type assignment because we
96
00:07:57,890 --> 00:08:04,370
got the types here already and this is a shortcut understood by Dart which tells Dart, okay what we receive
97
00:08:04,370 --> 00:08:07,490
as a name argument will be stored in a name property,
98
00:08:07,490 --> 00:08:11,260
what I receive as an age argument will be stored in the age property and
99
00:08:11,360 --> 00:08:16,530
now since I renamed this to name again because now the names here have to match, since I rename this
100
00:08:16,530 --> 00:08:23,060
to name, I also have to rename it down there of course. And now this works again and it's of course shorter
101
00:08:23,240 --> 00:08:27,050
than writing that extra function body for the constructor.
11358
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.