A_ N_
A_ N_

Reputation: 13

Getting incomplete data from mock API in emulator

I'm making a simple project to learn REST API integration in flutter, the project was almost done and I made the whole thing while running in web and it worked perfectly, but now when I run it in emulator, the data from user API does not show and by using debugPrint it shows that the incomplete data is fetched. The program is not showing any error and I do not understand why is it not working with the emulator, I also connected my own smartphone and the same issue is in my phone as well. I also used another Posts mock API and it is working just fine. This is the method I used to fetch data:

Future<List<Users>> getUsers() async {
    try{
      var response =await http.get(Uri.parse(baseUrl));
      debugPrint("Get User Response");
      debugPrint(response.body);
      debugPrint(response.statusCode.toString());

      if(response.statusCode == 200){
        var jsonData = jsonDecode(response.body);
        debugPrint("this is jsonData $jsonData");
        final json_ = jsonData['users'] as List<dynamic>;
        List<Users> users = json_.map((e) => Users.fromMap(e)).toList();
        debugPrint("this is users $users");
        return users;
      }
      else{
        return Future.error("Server Error");
      }
    }
    catch(e){
      return Future.error("Error Fetching Data");
    }
  }

this is what the debugPrint() is printing

I/flutter (22977): Get User Response
I/flutter (22977): {"users":[{"id":1,"firstName":"Terry","lastName":"Medhurst","maidenName":"Smitham","age":50,"gender":"male","email":"[email protected]","phone":"+63 791 675 8914","username":"atuny0","password":"9uQFF1Lh","birthDate":"2000-12-25","image":"https://robohash.org/hicveldicta.png","bloodGroup":"A−","height":189,"weight":75.4,"eyeColor":"Green","hair":{"color":"Black","type":"Strands"},"domain":"slashdot.org","ip":"117.29.86.254","address":{"address":"1745 T Street Southeast","city":"Washington","coordinates":{"lat":38.867033,"lng":-76.979235},"postalCode":"20020","state":"DC"},"macAddress":"13:69:BA:56:A3:74","university":"Capitol University","bank":{"cardExpire":"06/22","cardNumber":"50380955204220685","cardType":"maestro","currency":"Peso","iban":"NO17 0695 2754 967"},"company":{"address":{"address":"629 Debbie Drive","city":"Nashville","coordinates":{"lat":36.208114,"lng":-86.58621199999999},"postalCode":"37076","state":"TN"},"department":"Marketing","name":"Blanda-O'Keefe","title":"Help Desk Operator"},"ein":
I/flutter (22977): 200
I/flutter (22977): this is jsonData {users: [{id: 1, firstName: Terry, lastName: Medhurst, maidenName: Smitham, age: 50, gender: male, email: [email protected], phone: +63 791 675 8914, username: atuny0, password: 9uQFF1Lh, birthDate: 2000-12-25, image: https://robohash.org/hicveldicta.png, bloodGroup: A−, height: 189, weight: 75.4, eyeColor: Green, hair: {color: Black, type: Strands}, domain: slashdot.org, ip: 117.29.86.254, address: {address: 1745 T Street Southeast, city: Washington, coordinates: {lat: 38.867033, lng: -76.979235}, postalCode: 20020, state: DC}, macAddress: 13:69:BA:56:A3:74, university: Capitol University, bank: {cardExpire: 06/22, cardNumber: 50380955204220685, cardType: maestro, currency: Peso, iban: NO17 0695 2754 967}, company: {address: {address: 629 Debbie Drive, city: Nashville, coordinates: {lat: 36.208114, lng: -86.58621199999999}, postalCode: 37076, state: TN}, department: Marketing, name: Blanda-O'Keefe, title: Help Desk Operator}, ein: 20-9487066, ssn: 661-64-2976, userAgent: Mozilla/5.0 (Window

and this is my fromMap method

factory Users.fromMap(Map<String, dynamic> map) {
    return Users(
      id: map['id'] as int,
      firstName: map['firstName'] as String,
      lastName: map['lastName'] as String,
      maidenName: map['maidenName'] as String,
      age: map['age'] as int,
      gender: map['gender'] as String,
      email: map['email'] as String,
      phone: map['phone'] as String,
      username: map['username'] as String,
      password: map['password'] as String,
      birthDate: map['birthDate'] as String,
      image: map['image'] as String,
      bloodGroup: map['bloodGroup'] as String,
      height: map['height'] as num,
      weight: map['weight'] as double,
      eyeColor: map['eyeColor'] as String,
      hair: map['hair'] as Map,
      domain: map['domain'] as String,
      ip: map['ip'] as String,
      address: map['address'] as Map,
      macAddress: map['macAddress'] as String,
      university: map['university'] as String,
      bank: map['bank'] as Map,
      company: map['company'] as Map,
      ein: map['ein'] as String,
      ssn: map['ssn'] as String,
      userAgent: map['userAgent'] as String,
    );
  } //

But I do know it's not an issue with the fromMap method since everything works just fine on web but it has something to do with the incomplete response because since the response is incomplete the fromMap method doesn't work it is not being converted to user instance

User API = https://dummyjson.com/users
Posts API = https://dummyjson.com/posts

Upvotes: 1

Views: 64

Answers (0)

Related Questions