Home > Enterprise >  Flutter independent model classes
Flutter independent model classes

Time:08-26

I'm new to the flutter development. I'm developing a simple weather application then I face the below question. 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 -return 'temp' and API 2 - return 'temperature' and I want to use the same model class. Please help me to solve this problem.

CodePudding user response:

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

CodePudding user response:

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