颤振错误无法将参数类型'Map <String,dynamic> Function()'分配给参数类型'Map <String,dynamic>'

时间:2020-09-09 10:49:06

标签: flutter

我试图在颤动中创建一些图表,我陷于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);
        }
      },
    );
  }

2 个答案:

答案 0 :(得分:1)

根据FlutterFire API docsdata的{​​{1}}成员是一个返回映射的函数;因此需要调用它以在地图中返回快照的数据:

DocumentSnapshot

请注意,我将参数名称更改为 List<Sales> sales = snapshot.data.docs .map((shapshot) => Sales.fromMap(snapshot.data())) .toList(); ,以提高代码的可读性并避免意外使用类名。

答案 1 :(得分:0)

          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,
                        );
                      }
                    }
                  },
                )

我是这样实现的,以便为我的聊天应用程序检索数据。我认为该代码对您有用。试试吧

相关问题