【问题标题】:How to solve "The property 'length' can't be unconditionally accessed because the receiver can be 'null'"如何解决“无法无条件访问属性'length',因为接收者可以为'null'”
【发布时间】:2021-08-05 21:53:36
【问题描述】:

我也为接收者添加了不为空的条件,但错误仍然存​​在。这是我无法解决的 2 个错误。请帮忙!我正在制作“Notes”应用程序,我在 sqflite db 中存储值。

        body: FutureBuilder(
          future: getNotes(),
          builder: (context, noteData) {
            switch (noteData.connectionState) {
              case ConnectionState.waiting:
                {
                  return Center(child: CircularProgressIndicator());
                }
              case ConnectionState.done:
                {
                  if (noteData.data == null) {
                    return Center(
                      child: Text("You don't have any notes yet, create one!"),
                    );
                  } else {
                    return Padding(
                      padding: EdgeInsets.all(8.0),
                      child: ListView.builder(
                         itemCount: noteData.data.length,   //Error
                        itemBuilder: (context, index) {
                          String title = noteData.data[index]['title'];// Error-The method '[]' can't be 
                                                                  //unconditionally invoked because the receiver //can be 'null'.
                          String body = noteData.data[index]['body'];
                          String creation_date =
                              noteData.data[index]['creation_date'];
                          int id = noteData.data[index]['id'];

             

【问题讨论】:

    标签: flutter dart sqflite


    【解决方案1】:

    这与 Dart 空安全性有关。解决此问题的一种方法是使用 bang 运算符,因为您确定 noteData.data 永远不会为空(您在使用前检查),例如:

    noteData.data!.lengthnoteData.data![index]['title'] 等等。

    这个解决方案可能看起来很麻烦,所以我建议在使用 noteData.data 之前创建一个局部变量:

    ...
    } else {
      final data = noteData.data; // noteData.data! could be needed here, not sure
    
      return Padding(
        padding: EdgeInsets.all(8.0),
          child: ListView.builder(
            itemCount: data.length,
            ...
          ),
        ),
      );
    }
    

    【讨论】:

    • 它不起作用。它再次显示相同的错误。我也使用了 bang 运算符,但它导致了同样的问题
    • 那么noteData 之后可能还需要bang 运算符?如果您发布getNotes()方法的代码会有所帮助。
    • 我已经添加了他的代码。请查看@mkobuolys
    • 哦,我明白了...首先,您应该重构代码以返回 List 而不是动态的,这样以后会更容易处理。
    【解决方案2】:
    
      Future<dynamic> getNotes() async {
        final db = await database;
        var res = await db!.query("notes");
        if (res.length == 0) {
          return null;
        } else {
          var resultMap = res.toList();
          return resultMap.isNotEmpty ? resultMap : null;
        }
      }
    

    这是我的笔记课

    class NoteModel {
      final int? id;
      final String? title;
      final String? body;
      final DateTime? creation_date;
    
      NoteModel({this.id, this.title, this.body, this.creation_date});
    
      Map<String, dynamic> toMap() {
        return ({
          "id": id,
          "title": title,
          "body": body,
          "creation_date": creation_date
        });
      }
    }
    

    【讨论】:

      【解决方案3】:

      有同样的错误,当我把 "AsyncSnapshot" 放在 noteData 之前解决了我的错误

      【讨论】:

        猜你喜欢
        • 2021-09-21
        • 2021-07-13
        • 2022-08-15
        • 2022-08-15
        • 1970-01-01
        • 2022-08-15
        • 2022-01-11
        • 1970-01-01
        • 2021-09-21
        相关资源
        最近更新 更多