Ok, let's break this down:
Response response = await
get(Uri.parse('https://worldtimeapi.org/api/timezone/Europe/London'));
A variable declaration in Dart looks like this:
<Type> <name> [= <value>];
So in your case Response
is the type of the variable, response
is the name of the variable and get(Uri.parse('https://worldtimeapi.org/api/timezone/Europe/London'))
is the value of the variable (actually get(...)
is a future, and the future's response is the value, that's why the await
keyword is there, but that's not important.)
Sidenote, you can actually use the var
keyword to skip the <Type>
part of the declaration:
int myFunction(a, b) => a+b;
int x = myFunction(1,2);
Above, we know myFunction
returns an int, and the variable x
is equal to the result of myFunction
, so it must also be an int, we can use the var
keyword to skip writing int then:
int myFunction(a, b) => a+b;
var x = myFunction(1,2);
Of course, here there isn't that big a difference between writing int
and var
, but when your type is something like List<Map<String, List<int>>>
it is quite nice to be able to skip writing that over and over
Now for this line:
DateTime now = DateTime.parse(datetime);
We already know that the first DateTime
tells us what type the variable is, and we know that now
is the name of the variable, we also know that the variable's value is DateTime.parse(datetime)
because it is what goes after the =
sign, but we still don't know what DateTime.parse(datetime)
means.
Classes can have static and non-static methods, a static method is a method that gets called on a class, while a non-static method gets called on an object. Most method calls ever are non-static, look at this example:
class Car {
void accelerate() {
// some method to increase the speed of the car
}
void decelerate() {
// some method to decrease the speed of the car
}
Car buyCar(int maxCost) {
// some method to buy a new car
}
}
void main() {
Car car = Car();
car.accelerate();
car.decelerate();
Car otherCar = car.buyCar(100000000);
}
Above, the method accelerate
and decelerate
are instance methods and that makes sense because they probably affect some statistics of the car (like current speed), buyCar
is also an instance method, but if you think about it, it shouldn't be, it doesn't affect your current car if you buy a new one and also you shouldn't need to have a car object to buy another one in the first place. So let's make that last method static:
class Car {
void accelerate() {
// some method to increase the speed of the car
}
void decelerate() {
// some method to decrease the speed of the car
}
static Car buyCar(int maxCost) {
// some method to buy a new car
}
}
It is as simple as adding the static
keyword, now instead of having to do this:
Car myOldCar = Car();
Car myNewCar = myOldCar.buyCar(100000000);
we can just do this:
Car myNewCar = Car.buyCar(100000000);
looks familiar?
That's right parse
is a static method on DateTime
class, it takes a string that looks like this: 2012-02-27 13:27:00.123456789z
and returns a DateTime
object.
So to recap:
Response response = await
get(Uri.parse('https://worldtimeapi.org/api/timezone/Europe/London'));
Response
is the type of the variable, response
is its name and get(Uri.parse('https://worldtimeapi.org/api/timezone/Europe/London'))
is a method that returns a Response
object.
DateTime now = DateTime.parse(datetime);
Similarly DateTime
is the type, now
is the name and DateTime.parse
is a static
method that parses a string and makes a DateTime
object, in this case the string is datetime
, which was declared as being equal to data['datetime']
.
If you want to understand better how the parse
method works, here is the DateTime parse documentation