dart 如何解决“接收方可以为'null',属性'length'无法无条件访问”

v9tzhpje  于 2023-02-01  发布在  其他
关注(0)|答案(4)|浏览(162)

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

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'];
j8yoct9x

j8yoct9x1#

出现了相同的错误,当我在noteData之前放置***“AsyncSnapshot”***时,我的错误得到了解决

hfsqlsce

hfsqlsce2#

这与Dart null安全有关。解决这个问题的一个方法是使用bang操作符,因为您确信noteData.data永远不会为null(您在使用之前检查),例如:
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,
        ...
      ),
    ),
  );
}
wfveoks0

wfveoks03#

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

yshpjwxd4#

我也有同样的问题。在我的例子中,我忘记了把列表〈〉放在未来之后

return FutureBuilder<List<Video>>(
        future: _listarVideos(),
        builder: (context, snapshot){
          switch(snapshot.connectionState){
            case ConnectionState.none:
              return Center(
                child: CircularProgressIndicator(),
              );
              break;

相关问题