这是我的第一个问题,我对编码并不专业,只是为了发展自己,所以我希望自己能自己解释一下。
我一直在尝试开发社交平台,但在与Firebase连接的用户模型上遇到麻烦。您可以在下面看到我的用户模型:
import 'package:cloud_firestore/cloud_firestore.dart';
class User {
final String id;
final String profileName;
final String username;
final String url;
final String email;
final String bio;
User({
this.id,
this.profileName,
this.username,
this.url,
this.email,
this.bio,
});
factory User.fromDocument(DocumentSnapshot doc){
return User(
id: doc.id,
profileName: doc['profileName'],
username: doc['username'],
url: doc['photoUrl'],
email: doc['email'],
bio: doc['bio'],
);
}
}
当我这样创建用户模型时,我得到“没有为'DocumentSnapshot'类型定义运算符'[]'。”您可以在屏幕截图上看到错误。我花了3天的时间找到解决方案,但在互联网上找不到类似的问题。你能帮我吗?
最佳答案
或者尝试在数据后添加()
factory User.fromDocument(DocumentSnapshot doc){
return User(
id: doc.id,
profileName: doc.data()['profileName'],
username: doc.data()['username'],
url: doc.data()['photoUrl'],
email: doc.data()['email'],
bio: doc.data()['bio'],
);
}