Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,080 --> 00:00:05,370
In the last section, we got a better idea of how to work with Jason data that we fetch from some remote
2
00:00:05,370 --> 00:00:11,190
API, but the very end, I mentioned how with Dart we like to have a good idea of the type of data that
3
00:00:11,190 --> 00:00:15,000
we are working with when we retrieve some JSON from some endpoint.
4
00:00:15,120 --> 00:00:19,740
We really don't know what the values that we're going to get back inside that, Jason, are.
5
00:00:20,220 --> 00:00:26,160
So one very common thing that we do is create model classes to give darte a better idea of the type
6
00:00:26,160 --> 00:00:29,490
of data that we expect to get back from this JSON API.
7
00:00:31,240 --> 00:00:36,490
So to get started in creating a model class will first define a class at the bottom of this file.
8
00:00:37,590 --> 00:00:43,560
I'm going to call my class image model because it is essentially a class that's going to model all the
9
00:00:43,560 --> 00:00:45,300
different properties of an image.
10
00:00:46,340 --> 00:00:51,500
Then the two different instance variables that I will assign to this class are going to be the same
11
00:00:51,500 --> 00:00:54,050
instance, variables that are contained inside of my JSON.
12
00:00:54,740 --> 00:00:56,290
So I care about this property.
13
00:00:56,300 --> 00:00:59,150
You are are right here and this is property right here.
14
00:00:59,810 --> 00:01:04,819
You, Earl, is supposed to be a string and it over here is supposed to be an integer.
15
00:01:05,360 --> 00:01:10,790
So I'm going to add an instance variable of Int for ID and for string.
16
00:01:11,120 --> 00:01:12,350
I'll put in a Eurail.
17
00:01:13,720 --> 00:01:20,080
So now we can add in a constructor function that will take in some ID and some URL and create a new
18
00:01:20,080 --> 00:01:22,510
instance of this image model that we can work with.
19
00:01:23,350 --> 00:01:25,170
So I will define my constructor function.
20
00:01:25,480 --> 00:01:30,400
Remember, we can do that by defining a function inside the class with an identical name to the class
21
00:01:30,400 --> 00:01:30,910
itself.
22
00:01:31,770 --> 00:01:38,370
I'll put a semicolon at the end of the line and then we can accept inside of here this dotted as the
23
00:01:38,370 --> 00:01:41,850
first argument in this URL as the second argument.
24
00:01:44,420 --> 00:01:46,670
So now, rather than doing the print statement up here.
25
00:01:47,850 --> 00:01:55,770
I can try to make a new image model instance with a new image model, and then for the first argument,
26
00:01:55,770 --> 00:02:03,780
I will pass in an ID, so I will say past Jason and I'll use the square brackets ID.
27
00:02:04,890 --> 00:02:11,260
And then for the second argument, I'll pass in that you out so fast, Jason, you, Earl.
28
00:02:12,000 --> 00:02:12,300
All right.
29
00:02:12,320 --> 00:02:15,090
I know this line is a little bit tight right there.
30
00:02:15,090 --> 00:02:19,470
So I'm going to zoom out for just a moment so you can see the entire segment of code on one line.
31
00:02:21,630 --> 00:02:22,160
There we go.
32
00:02:23,880 --> 00:02:29,910
So now when we get back, this map of past JSON data will create a new image model instance passing
33
00:02:29,910 --> 00:02:37,380
in our ID and the URL, now that we've got the image model right here, Darte knows exactly what properties
34
00:02:37,380 --> 00:02:41,520
and what types of values it should expect to see on this image model.
35
00:02:42,180 --> 00:02:48,750
So now we could print out, say, image model and we do not have to access our ID or our URL with the
36
00:02:48,750 --> 00:02:50,180
bracket notation anymore.
37
00:02:50,220 --> 00:02:57,810
We can simply use a print image model ID and if we run this, we will again see one appear up here.
38
00:02:58,560 --> 00:02:59,060
There we go.
39
00:02:59,670 --> 00:03:01,770
And of course, I can do your URL as well.
40
00:03:03,850 --> 00:03:05,920
And I get back the out.
41
00:03:07,150 --> 00:03:11,380
OK, I'm going to zoom in just one more time and we're going to look at one additional improvement that
42
00:03:11,380 --> 00:03:12,280
we can make to this.
43
00:03:13,380 --> 00:03:16,170
All right, so nice and big again, so you can see the code more easily.
44
00:03:17,370 --> 00:03:22,620
So all the kids we have right now definitely works, and it definitely gives start a better idea of
45
00:03:22,620 --> 00:03:26,590
the different properties that we expect to get back from our Jason and their different types.
46
00:03:27,330 --> 00:03:32,430
However, you'll notice that whenever we create an instance of the image model right here, we have
47
00:03:32,430 --> 00:03:38,190
to do this very laborious practice of referencing the past, Jason, and then trying to reference some
48
00:03:38,190 --> 00:03:39,760
particular property to pass in.
49
00:03:40,230 --> 00:03:46,680
So I want to make one more improvement to this, and I want to try to make creation of an image model
50
00:03:46,680 --> 00:03:50,420
like we are right here a little bit easier and straightforward than it is right now.
51
00:03:50,820 --> 00:03:52,290
So let's take one more brief pause.
52
00:03:52,500 --> 00:03:56,040
We'll come back to the next section and we'll figure out how we can improve this line of code right
53
00:03:56,040 --> 00:03:58,890
here to be a little bit more concise than it currently is.
54
00:03:59,580 --> 00:04:01,530
So quick break and I'll see you in just a minute.
5590
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.