Database 如何从firestore Flatter中获取整数_TypeError(type';String';不是int类型的子类型)
用户_repo.dartDatabase 如何从firestore Flatter中获取整数_TypeError(type';String';不是int类型的子类型),database,firebase,flutter,number-formatting,Database,Firebase,Flutter,Number Formatting,用户_repo.dart Future<List<UserModel>> fetchUsers() async { var usercollection = await users.get(); var userList = usercollection.docs.map((e) => EU.fromSnapShot(e)).toList(); print("repo: ${userList[1].avgRating}"
Future<List<UserModel>> fetchUsers() async {
var usercollection = await users.get();
var userList = usercollection.docs.map((e) => EU.fromSnapShot(e)).toList();
print("repo: ${userList[1].avgRating}");
return userList;
}
}
获取avgRating时出错,avgRating作为数字存储在Cloud Firestore上。我试过了
我非常感谢你的帮助。谢谢所以我在将值声明为“var avgRating”时尝试更改数据类型,结果成功了。如果我用int或double声明它,它会给出相同的错误。我认为,因为firestore的数字类型支持多种数据类型,比如int、double
谢谢你的帮助 什么是快照.data()['avg_rating']类型?int还是String?您是否尝试int.parse(snapshot.data()['avg_rating']);没有toString?是的,我在没有toString的情况下试过。toString没用。它的号码是从firestore检索到的。
class UserModel{
final String user_id;
final String username;
String currentProfile = '--';
double yearsOfExp = 0;
String smallIntro = "";
String emailId = "";
String image;
int avgRating;
EU({
this.user_id,
this.username,
this.currentProfile,
this.smallIntro,
this.yearsOfExp,
this.emailId,
this.image,
this.avgRating}) ;
UserModel.fromSnapShot(DocumentSnapshot snapshot)
: assert(snapshot != null),
eu_id =
snapshot.data()['userId'] != null ? snapshot.data()['userId'] : 'null',
username = snapshot.data()['username'],
image = snapshot.data()['profile_img_url'],
emailId = snapshot.data()['email'],
avgRating = snapshot.data()['avg_rating'];
}