[英]The method 'data' isn't defined for the type 'Object'. Try correcting the name to the name of an existing method, or defining a method named 'data'
I have issues to display data from Firebase. Here is the code I used in my FutureBuilder.我在显示来自 Firebase 的数据时遇到问题。这是我在 FutureBuilder 中使用的代码。 Looks like there is an error regarding data(), but I don't know which one, does anyone have any idea?
看起来有关于 data() 的错误,但我不知道是哪一个,有人知道吗?
This is what I get: "The method 'data' isn't defined for the type 'Object'. Try correcting the name to the name of an existing method, or defining a method named 'data'."这就是我得到的:“未为类型‘对象’定义方法‘数据’。尝试将名称更正为现有方法的名称,或定义名为‘数据’的方法。”
if(snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> documentData = snapshot.data!.data() as Map<String, dynamic>;
return ListView(
children: [
CustomSubtitle(
text: "${documentData['01 - Brand']}"
),
CustomTitle(
text: "${documentData['02 - Name']}",
),
CustomText(
text: "${documentData['04 - Description']}",
)
],
);
}
You are using code for a QuerySnapshot
.您正在使用
QuerySnapshot
的代码。 For such one your code would work but considering how you use the resulting data you received a DocumentSnapshot
.对于这样的代码,您的代码可以工作,但考虑到您如何使用您收到的结果数据
DocumentSnapshot
。 For that to work just change your code to this:为此,只需将您的代码更改为:
if(snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> documentData = snapshot.data as Map<String, dynamic>;
return ListView(
children: [
CustomSubtitle(
text: "${documentData['01 - Brand']}"
),
CustomTitle(
text: "${documentData['02 - Name']}",
),
CustomText(
text: "${documentData['04 - Description']}",
)
],
);
}
If someone is in my case, I've found the solution:如果有人是我的情况,我已经找到了解决方案:
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if(snapshot.connectionState == ConnectionState.done) {
DocumentSnapshot<Object?> documentData = snapshot.data!;
return ListView(
children: [
CustomSubtitle(
text: "${documentData['01 - Brand']}"
),
CustomTitle(
text: "${documentData['02 - Name']}",
),
CustomText(
text: "${documentData['04 - Description']}",
)
],
);
}
}
future: collection.doc(documentId).get(),
builder: ((context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data =
snapshot.data!.data() as Map<String, dynamic>;
String currency = '${data['currency']}';
return Material(
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.