【问题标题】:Flutter error The argument type 'Map<String, dynamic> Function()' can't be assigned to the parameter type 'Map<String, dynamic>'Flutter 错误参数类型'Map<String, dynamic> Function()' 不能分配给参数类型'Map<String, dynamic>'
【发布时间】:2020-12-27 18:59:36
【问题描述】:

我正在尝试在颤振中创建一些图表,我陷入了 DocumentSnapshot.data 我觉得颤振已经改变了命名但我在任何地方都找不到它。

Widget _buildBody(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: FirebaseFirestore.instance.collection('sales').snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return LinearProgressIndicator();
        } else {
  
          List<Sales> sales = snapshot.data.docs
              .map((documentSnapshot) => Sales.fromMap(DocumentSnapshot.data))
              .toList();
          return _buildChart(context, sales);
        }
      },
    );
  }

【问题讨论】:

  • 错误出现在哪里?也许这个问题的答案会有所帮助:stackoverflow.com/questions/56721694/… 即使用 documentSnapshot.data.data 获取数据地图
  • 此外,Sales.fromMap(DocumentSnapshot.data) 中可能存在拼写错误,因为您似乎使用的是类名而不是实例。

标签: flutter


【解决方案1】:

根据FlutterFire API docsdata的成员DocumentSnapshot是一个返回map的函数;因此需要调用它以在地图中返回快照的数据:

       List<Sales> sales = snapshot.data.docs
          .map((shapshot) => Sales.fromMap(snapshot.data()))
          .toList();

请注意,我将参数名称更改为snapshot,以使代码更具可读性并避免意外使用类名。

【讨论】:

  • 谢谢伯,它工作正常!非常感谢您的帮助。
【解决方案2】:
          StreamBuilder(
                  stream: Firestore.instance
                      .collection('users')
                      .document(id)
                      .collection('chatWith')
                      .orderBy('timestamp', descending: true)
                      .snapshots(),

                  builder: (context, snapshot) {
                    if (!snapshot.hasData) {
                      return Center(
                        child: CircularProgressIndicator(
                          valueColor: AlwaysStoppedAnimation<Color>(
                              AppColor.colorCustom),
                        ),
                      );
                    } else {
                      if (snapshot.data.documents.length == 0) {
                        return Container(
                          alignment: Alignment.center,
                          child: Text(
                            "No Chat History Found",
                            style: TextStyle(
                              color: Colors.grey,
                              fontWeight: FontWeight.normal,
                              fontSize:
                                  Util.px_23 * SizeConfig.textMultiplier,
                              fontFamily: 'Roboto',
                            ),
                            softWrap: true,
                          ),
                        );
                      } else {
                        return ListView.builder(
                          padding: EdgeInsets.all(
                              Util.px_10 * SizeConfig.heightMultiplier),
                          itemBuilder: (context, index) => _listItem(
                              context, snapshot.data.documents[index]),
                          itemCount: snapshot.data.documents.length,
                        );
                      }
                    }
                  },
                )

我这样实现来检索我的聊天应用程序的数据。我认为该代码对您有用。试试看

【讨论】:

    猜你喜欢
    • 2021-03-23
    • 2020-12-15
    • 2021-08-29
    • 2021-10-02
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2021-08-03
    相关资源
    最近更新 更多