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.
CodePudding user response:
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 ListOfPackages
s, which you could then assign to activatedPackages
.
CodePudding user response:
So this is what i tried!
UserModel _userDataFromSnapshot(DocumentSnapshot doc) {
List<ListOfPackages> packagesData = [];
List<ListOfPackages> _activatedPackages(
Map<String, dynamic> activatedPackages) {
packagesData = activatedPackages.entries
.map((e) => ListOfPackages(
acceptedPackage: e.value['acceptedPackages'],
packageId: e.value['packageId'],
packageTime: e.value['packageTime'],
proofImage: e.value['proofImage'],
uid: e.value['uid'],
username: e.value['username'],
))
.toList();
return packagesData;
}
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.data().toString().contains('activatedPackages') ? _activatedPackages(doc.get('activatedPackages')) : [],
uid: doc.get('uid') ?? '');
}
but that is giving an exception.
StreamProvider<UserModel>, but no `catchError` was provided.
Exception:
type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'