firebase 没有为类型“Object”定义方法“data”,请尝试将名称更正为现有方法的名称,或定义名为“data”的方法

khbbv19g  于 2022-12-14  发布在  其他
关注(0)|答案(3)|浏览(167)

我在显示Firebase的数据时遇到了问题。这是我在FutureBuilder中使用的代码。看起来有一个关于data()的错误,但我不知道是哪一个,有人知道吗?
这是我得到的:没有为类型“Object”定义方法“data.“请尝试将名称更正为现有方法得名称,或定义一个名为”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']}",
         )

       ],
     );
   }
idfiyjo8

idfiyjo81#

您正在使用QuerySnapshot的代码。对于这样的代码,您的代码可以工作,但要考虑如何使用您收到的DocumentSnapshot结果数据。要使其工作,只需将您的代码更改为:

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']}",
         )

       ],
     );
   }
w9apscun

w9apscun2#

如果有人在我的情况下,我已经找到了解决办法:

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']}",
           )

         ],
       );
     }
}
rkkpypqq

rkkpypqq3#

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(
          )

相关问题