user16017178
user16017178

Reputation: 113

convereting List of Map into Dart Objects

i have a list of map items in firebase.. like this

{
  "0": [
    {
      "score": 4.5,
      "review": "The pizza was amazing!"
    },
    {
      "score": 5.0,
      "review": "Very friendly staff, excellent service!"
    }
  ],
"1": [
    {
      "score": 4.5,
      "review": "The pizza was amazing!"
    },
    {
      "score": 5.0,
      "review": "Very friendly staff, excellent service!"
    }
  ]
}

I cant convert it into dart objects correctly... this is just an example my data is different i tried this

  final String uid;
  final String name;
  final String email;
  final bool isAdmin;
  final String easypaisa;
  final String jazzCash;
  final String bankAccount;
  final String phoneNumber;
  final String profileImage;
  final List<String>? isFavorite;
  final List<ListOfPackages> activatedPackages;

  UserModel({
    this.uid = '',
    this.name = '',
    this.email = '',
    this.isAdmin = false,
    this.easypaisa = '',
    this.jazzCash = '',
    this.bankAccount = '',
    this.phoneNumber = '',
    this.profileImage = '',
    final List<String>? isFavorite,
    final List<ListOfPackages>? activatedPackages,
  })  : isFavorite = isFavorite ?? [],
        activatedPackages = activatedPackages ?? [];
}


class ListOfPackages {
  final bool acceptedPackage;
  final String packageId;
  final String packageTime;
  final String proofImage;
  final String uid;
  final String username;

  ListOfPackages(
      {this.acceptedPackage = false,
      this.packageId = '',
      this.packageTime = '',
      this.proofImage = '',
      this.uid = '',
      this.username = ''});
}

and here i'm mapping the data from firestore to the UserModel

    return UserModel(
        name: doc.get("name"),
        email: doc.get('email'),
        isAdmin: doc.get('isAdmin'),
        easypaisa: doc.get('easypaisa'),
        jazzCash: doc.get('jazzCash'),
        bankAccount: doc.get('bankAccount'),
        phoneNumber: doc.get('phoneNumber'),
        profileImage: doc.get('profilePic'),
        isFavorite: List.from(doc.data().toString().contains('favoritePackages')
            ? doc.get('favoritePackages')
            : []),
        activatedPackages: List.from(
            doc.data().toString().contains('activatedPackages')
                ? doc.get('activatedPackages')
                : []),
        uid: doc.get('uid') ?? '');
  }

With this, i'm getting this error Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'ListOfPackages' Can anyone guide me the right way to convert these? Actually most of the tutorials are in FactoryConstructor way so i can't take help from there... I'd appreciate any help.

Upvotes: 0

Views: 114

Answers (2)

user16017178
user16017178

Reputation: 113

found the solution!

 UserModel _userDataFromSnapshot(DocumentSnapshot doc) {
    return UserModel(
        name: doc.get("name"),
        email: doc.get('email'),
        isAdmin: doc.get('isAdmin'),
        easypaisa: doc.get('easypaisa'),
        jazzCash: doc.get('jazzCash'),
        bankAccount: doc.get('bankAccount'),
        phoneNumber: doc.get('phoneNumber'),
        profileImage: doc.get('profilePic'),
        isFavorite: List.from(doc.data().toString().contains('favoritePackages')
            ? doc.get('favoritePackages')
            : []),
        activatedPackages: (doc.get('activatedPackages') as List<dynamic>)
            .map((item) => ListOfPackages(
                  acceptedPackage: item['acceptedPackage'],
                  packageId: item['packageId'],
                  packageTime: item['packageTime'],
                  proofImage: item['proofImage'],
                  uid: item['uid'],
                  username: item['uid'],
                ))
            .toList(),
        uid: doc.get('uid') ?? '');
  }

Upvotes: 0

esramish
esramish

Reputation: 327

Can you create a function that takes in a map and converts it to a ListOfPackages object? Then maybe you could do a forEach on doc.get(‘activatedPackages’) to add each ListOfPackages object one at a time to a List of ListOfPackagess, which you could then assign to activatedPackages.

Upvotes: 1

Related Questions