Search code examples
fluttermodelarchitecture

Flutter independent model classes


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?


Solution

  • 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
        );
      }