Flutter independent model classes

Viewed 60

I have created a weather model class with its parameters and I'm going to get data from the API.
But I'm a bit curious If I want to get data from 2 APIs with different parameters how do I use the same model classes?
Example: API 1 -> returns 'temp' and API 2 -> returns 'temperature' and I want to use the same model class.
Is this even possible? If so how should I do it?

2 Answers

Include all parameters from both api response and make params which are different in both Apis nullable.

use different fromJson in your model class: for example in one of them do this:

static Weather fromJsonOne(Map<String, Object> json) {
    return Weather(
      temp: json["temp"] as String
    );
  }

and in the other do this:

static Weather fromJsonTwo(Map<String, Object> json) {
    return Weather(
      temp: json["temperature"] as String
    );
  }
Related