简体   繁体   English

方法“数据”没有为类型“对象”定义。 尝试将名称更正为现有方法的名称,或定义名为“数据”的方法

[英]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.

相关问题 未定义的名称“DefaultfirebaseOptions”。 尝试将名称更正为已定义的名称,或定义名称 - Undefined name 'DefaultfirebaseOptions'. Try correcting the name to one that is defined, or defining the name 运算符“[]”不是为“对象”类型定义的。 尝试定义运算符“[]”。 尝试从 Firestore 获取数据时出错 - The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]'. Error when trying to get data from Firestore 运算符“[]”没有为类型“Object”定义。 尝试定义运算符 '[]' - The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]' 运算符“[]”没有为类型“Object”定义。 尝试为 Flutter 定义运算符“[]” - The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]' for Flutter 未为类型“_HomeState”定义方法“UserUid” - The method 'UserUid' isn't defined for the type '_HomeState' 未为类型“QuerySnapshot”定义运算符“[]”<object?> ' 尝试定义运算符 '[]' - Flutter</object?> - The operator '[]' isn't defined for the type 'QuerySnapshot<Object?>' Try defining the operator '[]' - Flutter Flutter FireStore:未为类型“DocumentReference”定义方法“where” - Flutter FireStore: The method 'where' isn't defined for the type 'DocumentReference' 运算符“[]”没有为类型“Object”定义。 尝试在 stream 生成器中定义运算符 '[]' 错误 - The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]' error in stream builder Flutter 未为类型“_LeaveApplicationState”定义方法“User” - Flutter The method 'User' isn't defined for the type '_LeaveApplicationState' 从 Firebase 读取时,“未为类型‘Object’定义方法‘cast’” - "The method 'cast' isn't defined for the type 'Object'" when reading from Firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM