Home > Enterprise >  Return multiple Objects from json with dart
Return multiple Objects from json with dart

Time:04-17

In a Flutter app I'm writing, I'm a beginner, I want to return 3 Objects from an http request. The http request works fine and returns data like:

{"one":{"fname":"Wyn","lname":"Owen","updated":1648076276673,"uid":"contact's uid","email":"[email protected]"},
"two":{"updated":1648076276673,"uid":"contact's uid","fname":"Roli","email":"[email protected]","lname":"Spider"},
"three":{"lname":"Poodle","email":"[email protected]","updated":1648076276673,"fname":"Bill","uid":"contact's uid"},
"user":{"name":null,"premium":false,"reg_id":"123456","created":1648076276673,"ads":true},
"channels":{"whatsapp":false,"email":true,"sms":false,"video":false,"app_msg":true},"shorts":{"expire":null,"code":"short_code"}}

It returns 5 KV pairs. The keys will always remain the same. All I'm interested are the 3 KVs: one, two and three. I want to create an object for each KV pair and call them one, two and three. I created the following model: Object Contacts contains 3 Person Objects:

 class Contacts {
      Contacts({
        required this.one,
        required this.two,
        required this.three,
      });
      Person one;
      Person two;
      Person three;
    
      factory Contacts.fromJson(Map<String, dynamic> json) => Contacts(
            one: json["one"],
            two: json["two"],
            three: json["three"],
          );
    
      Map<String, dynamic> toJson() => {
            "one": one.toJson(),
            "two": two.toJson(),
            "three": three.toJson(),
          };
    }
    
    class Person {
      Person({
        required this.uid,
        required this.updated,
        required this.email,
        required this.lname,
        required this.fname,
      });
    
      String uid;
      int updated;
      String email;
      String lname;
      String fname;
    
      factory Person.fromJson(Map<String, dynamic> json) => Person(
            uid: json["uid"],
            updated: json["updated"],
            email: json["email"],
            lname: json["lname"],
            fname: json["fname"],
          );
    
      Map<String, dynamic> toJson() => {
            "uid": uid,
            "updated": updated,
            "email": email,
            "lname": lname,
            "fname": fname,
          };
    }

This is the class I wrote:

class ContactsService {
  Future<List<Person>> fetchPersons(String uid) async {
    http.Response response =
        await http.get(Uri.parse("$PersonURL?uid=$uid"));

    if (response.statusCode == 200) {
      Map ContactData = jsonDecode(response.body);
      Person one = Person.fromJson(ContactData["one"]);
      Person two = Person.fromJson(ContactData["two"]);
      Person three = Person.fromJson(ContactData["three"]);

      List Persons = [];
      Persons.add(one);
      Persons.add(two);
      Persons.add(three);
      
      return Persons;
    } else {
      throw Exception("Something has gone wrong, ${response.statusCode}");
    }
  }
}

To get the objects in my Scaffold I would put

future: ContactsService.fetchPersons()

I want to be able to refer to one.fname, two.email etc I guess I'm missing something within fetchPersons(). So what is missing?

TIA

CodePudding user response:

You can solve it by changing

Map ContactData = jsonDecode(response.body);

to

final contacts = Contacts.fromJson(jsonDecode(a));

and

Person one = Person.fromJson(ContactData["one"]);
Person two = Person.fromJson(ContactData["two"]);
Person three = Person.fromJson(ContactData["three"]);

to

final one = contacts.one;
final two = contacts.two;
final three = contacts.three;

And as a supplement, serialization codes will be unnecessary if you use JsonSerializable provided by Google.

  • Related