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

问题描述

我在显示来自 Firebase 的数据时遇到问题。这是我在 FutureBuilder 中使用的代码。看起来关于 data() 有一个错误,但我不知道是哪一个,有人知道吗?

这就是我得到的:“方法'data'没有为'Object'类型定义。尝试将名称更正为现有方法的名称,或定义一个名为'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']}",
         )

       ],
     );
   }

标签: firebaseflutter

解决方案


如果有人在我的情况下,我找到了解决方案:

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

         ],
       );
     }
}

推荐阅读