1

I'm very new to programming and Dart. I was wondering why you put Response in front of the variable response. Also why do we use Datetime object in front of the variable 'now?' I believe when you want to instantiate, you write Datetime now = Datetime(); But it was written something else for the variable. What does it mean? Thank you very much in advance!

 void getTime() async{

    Response response = await get(Uri.parse('https://worldtimeapi.org/api/timezone/Europe/London'));
    Map data = jsonDecode(response.body);

    // get properties from data
    String datetime = data['datetime'];
    String offset = data['utc_offset'].substring(1,3);

    //create DateTime object
    DateTime now = DateTime.parse(datetime);
    now = now.add(Duration(hours: int.parse(offset)));
    print(now);

  }
dkackman
  • 15,179
  • 13
  • 69
  • 123
  • `Response` is the type of the variable `response`. Similarly `DateTime` is the type of the variable `now`. When you declare a variable you can either specify an explicit type (such as `Response`, `DateTime`, `int`, `String` etc..) or you can declare a variable with `var` or `final` and allow type inference to determine the type for you. More information here https://dart.dev/guides/language/language-tour#variables and here https://dart.dev/guides/language/type-system#type-inference – mmcdon20 Jan 16 '22 at 23:51

1 Answers1

0

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

h8moss
  • 4,626
  • 2
  • 9
  • 25