Flutter - 错误:没有为“Object”类型定义 getter“docs”

Edo*_*lep 9 dart flutter google-cloud-firestore

我正在开发 Flutter 2.2.1(通道稳定)。我最近将 SDK 的环境从 2.7.0 更改为 2.12.0 ( sdk: ">=2.12.0 <3.0.0") 以添加插件,但出现了很多错误(尤其是有关null safety 的错误)。其中之一是关于从 firestore 中提取数据(我正在使用cloud_firestore: ^2.2.1)。

我的代码:

StreamBuilder(
    stream: FirebaseFirestore.instance
        .collection('towns/${widget.townId}/beacons')
        .orderBy('monument')
        .snapshots(),
    builder: (ctx, snapshot) {
      if (snapshot.connectionState == ConnectionState.waiting)
        return CircularProgressIndicator();
      final beacons = snapshot.data!.docs; // Error here
      return ListView.builder(
          physics:
              NeverScrollableScrollPhysics(),
          shrinkWrap:
              true,
          itemCount: beacons.length,
          itemBuilder: (ctx, index) {
            if (beacons[index]['visibility'] == true) {
              return BeaconCard(
                title: beacons[index]['title'],
                monument: beacons[index]['monument'],
                image: beacons[index]['image'],
                duration: beacons[index]['duration'],
                distance: 0,
                townId: widget.townId,
                uuid: beacons[index]['uuid'],
                fontsizeValue: widget.fontsizeValue,
                languageId: widget.languageId,
              );
            }
            return Container();
          });
    }),
Run Code Online (Sandbox Code Playgroud)

错误大约docs在该行final beacons = snapshot.data!.docs;

没有为类型“Object”定义 getter“docs”。尝试导入定义“docs”的库,将名称更正为现有 getter 的名称,或者定义名为“docs”的 getter 或字段。

我是一个新的 flutter 用户,我不明白在这里该怎么做。感谢您的帮助。

Thi*_*lho 20

请提供预期的流类型。在这种情况下

StreamBuilder<QuerySnapshot>
Run Code Online (Sandbox Code Playgroud)


gen*_*ser 5

您需要声明构建器snapshot参数类型为AsyncSnapshot

例如:

StreamBuilder(
        stream: FirebaseFirestore.instance.collection('users').snapshots(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasData) {
            List<DocumentSnapshot> docs = snapshot.data!.docs;
            ...
Run Code Online (Sandbox Code Playgroud)