【问题标题】:Getting Unhandled Exception: type 'Null' is not a subtype of type 'Iterable<dynamic>' when trying to produce a feed出现未处理的异常:尝试生成提要时,类型 \'Null\' 不是类型 \'Iterable<dynamic>\' 的子类型
【发布时间】:2022-08-17 02:17:21
【问题描述】:

我正在尝试从 Firestore 检索提要,然后构建小部件并将它们呈现在包含在 futureBuilder 中的 ListView 中。 不幸的是,我不断收到此错误:

E/flutter (20020): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: type \'Null\' is not a subtype of type \'Iterable<dynamic>\'
E/flutter (20020): #0      _MomentsFeedState.getWidgetList (package:buzz/moments/moment_feed.dart:78:40)
E/flutter (20020): <asynchronous suspension>

这是我检索数据的函数:

  Stream<List<String>> getFriendUUIDS() => Firestore.friends
    .doc(gameManager.myself.uuid)
    .snapshots()
    .map((snapshot) => ((snapshot.data()?.keys)?.toList()) ?? []);

  Future<List<MomentWidget>> getWidgetList() async{
    List<MomentWidget> widgetList = [];
    Set<String> momentIds = Set();
    await for (final uuids in getFriendUUIDS()){
      for (String uuid in uuids){
        DocumentSnapshot<Map<String, dynamic>> values =  await Firestore.users
        .doc(uuid)
        .get();
        for (String momentId in values.data()?[\'moments\']){
          debugPrint(\'momentId: $momentId\');
          momentIds.add(momentId);
        }
      }
    }
    
    for (String momentId in momentIds){
      DocumentSnapshot<Map<String, dynamic>> values = 
      await Firestore.instance.collection(\'moments\').doc(momentId).get();
      Map<String, dynamic>? data = values.data()!;
      String downloadURL = await storage.ref(\'moments/$momentId\').getDownloadURL();
      MomentWidget widget = MomentWidget(numLikes: data[\'liked_by\'].length ,
       location: data[\'location\'],
        date: data[\'timestamp\'],
        names: data[\'taggedFriends\'].toString(),
        shotBy: data[\'taken_by\'], image: NetworkImage(downloadURL));
      widgetList.add(widget);
    }
    return widgetList;
  }

以及构建提要的功能:

Widget feed(){
    Size size = MediaQuery.of(context).size;
    return Container(
      height: size.height,
      width: size.width,
      child: FutureBuilder(
        future: getWidgetList(),
        builder: (context, AsyncSnapshot<List<MomentWidget>> snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.waiting:
              return Center(
                child: CircularProgressIndicator(),
              );
            case ConnectionState.done:
              if (snapshot.hasError) {
                return Text(snapshot.error.toString());
              } else {
                return ListView.builder(
                    shrinkWrap: true,
                    scrollDirection: Axis.vertical,
                    itemBuilder: (context, pos) {
                      return snapshot.data![pos];
                    },
                  );
              }
            default:
              return Text(\'Unhandled State\');
          }
        }
      ),
      );
  }

  @override
  Widget build(BuildContext context) {
    getWidgetList();
    return Scaffold(
      body: Container(
        width: MediaQuery.of(context).size.width,
        child: feed(),
      ),
    );
  }
}

我没有输出到屏幕。我是 Flutter 的新手,所以我不确定我是否犯了其他类型的错误。提前致谢!

    标签: flutter dart google-cloud-firestore flutter-listview flutter-futurebuilder


    【解决方案1】:

    看起来moments 可以为空,但没有检查它。在 for 循环中,如果 moments 为空,则使用空合并运算符 ?? 默认为空列表。

    它将类似于以下 sn-p:

    for (String momentId in values.data()?['moments'] ?? []){
      debugPrint('momentId: $momentId');
      momentIds.add(momentId);
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-14
      • 2021-09-20
      • 1970-01-01
      • 2021-08-01
      • 2021-07-21
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多