一切都很好,但是当我升级我的云 Firestore 依赖项时。我开始收到错误消息“未为类型 'Object' 定义运算符 '[]'。”。这个错误出现在所有 4 个 userData.data()[""] 之前,
class BaseProvider with ChangeNotifier {
//instances of firebase
final FirebaseAuth _auth = FirebaseAuth.instance;
final CollectionReference postsCollection =
FirebaseFirestore.instance.collection("posts");
final CollectionReference userCollection =
FirebaseFirestore.instance.collection("users");
//Creating post
Future addPost(
) async {
DocumentSnapshot userData =
await userCollection.doc(_auth.currentUser.uid).get();
return await postsCollection.doc().set({
"id": _auth.currentUser.uid,
"sellername": userData.data()["name"], //Error
"sellercontact": userData.data()["phone"], //Error
"sellercity": userData.data()["city"], //Error
"sellerstate": userData.data()["state"], //Error
});
}
Run Code Online (Sandbox Code Playgroud) 我的代码在以下错误中给出了我在此行中从 firebase 访问用户名时遇到的问题
snapshot.data['username']
它给出了上面提到的错误
我知道访问地图数据的唯一方法是这样
FutureBuilder<Object>(
future: FirebaseFirestore.instance
.collection('users')
.doc(userId)
.get(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading...");
}
return Text(
snapshot.data['username'],
style: TextStyle(
fontWeight: FontWeight.bold,
),
);
}
),
Run Code Online (Sandbox Code Playgroud)