【问题标题】:Future<List<Note>> is not a subtype of type List<Note> [duplicate]Future<List<Note>> 不是 List<Note> 类型的子类型 [重复]
【发布时间】:2022-01-05 15:20:41
【问题描述】:

我正在从 sqlite 获取数据并尝试在列表视图中设置它,但出现错误:

    type 'Future<List<Note>>' is not a subtype of type 'List<Note>'

这是我的sqlite查询函数:

Future<List<Note>> readAllNotes() async {
final db = await instance.database;

final orderBy = '${NoteFields.timeAdded} ASC';

final result = await db?.query(tableNotes, orderBy: orderBy);

return result!.map((json) => Note.fromJson(json)) as List<Note>;

}

这里是调用 readAllNotes() 的地方:

class NoteListWidget extends StatefulWidget {


const NoteListWidget({
    Key? key,
  }) : super(key: key);

  @override
  State<NoteListWidget> createState() => _NoteListWidgetState();
}

class _NoteListWidgetState extends State<NoteListWidget> {
  

  @override
  Widget build(BuildContext context) {
    List<Note> note = NotesDataBase.instance.readAllNotes() as List<Note>;
return ListView.builder(
    itemCount: note.toString().characters.length,
    itemBuilder: (ctx, index) {
      return NoteTileWidget(
        body: note[index].body,
        title: note[index].title,
        timeAdded:
            ('${note[index].timeAdded.day}/${note[index].timeAdded.month}/${note[index].timeAdded.year}'),
        id: note[index].id,
      );
    });

} }

【问题讨论】:

    标签: android ios flutter dart future


    【解决方案1】:

    你必须使用FutureBuilder:

        class _NoteListWidgetState extends State<NoteListWidget> {
          @override
          Widget build(BuildContext context) {
            return FutureBuilder<List<Note>>(
              future: NotesDataBase.instance.readAllNotes(),
              builder: (context, snapshot) {
                 if (snapshot.hasData){
                   final List<Note> notes = snapshot.data;
                   return ListView.builder(
                       itemCount: notes.length,
                       itemBuilder: (ctx, index) {
                         return NoteTileWidget(
                           body: notes[index].body,
                           title: notes[index].title,
                           timeAdded:
                           ('${notes[index].timeAdded.day}/${notes[index].timeAdded.month}/${notes[index].timeAdded.year}'),
                           id: notes[index].id,
                         );
                       });
                 }else{
                   return const CircularProgressIndicator();
                 }
               
              }
            );
          }
        }
    

    【讨论】:

    • 构建小部件不可能是未来,因此它给了我一个错误
    • 在这种情况下,您必须使用 FutureBuilder 包装您的 ListView.builder。我已经编辑了我的答案。
    • 我真的不知道你是从你编辑的代码中的哪里得到的注释
    • 对不起,我没有创建 Note 类,所以我没有测试它。我再次编辑了我的代码,它现在应该可以工作了
    • 成功了,非常感谢您抽出宝贵的时间,我是一名初级跨平台移动开发人员,我真的需要一份实习/兼职工作。我真的很感谢你的帮助。非常感谢
    【解决方案2】:
    class NoteListWidget extends StatefulWidget {
      const NoteListWidget({
        Key? key,
      }) : super(key: key);
    
      @override
      State<NoteListWidget> createState() => _NoteListWidgetState();
    }
    
    class _NoteListWidgetState extends State<NoteListWidget> {
      @override
      Widget build(BuildContext context) {
        return FutureBuilder(
            future: NotesDataBase.instance.readAllNotes(),
            builder: (context, dataSnapshot) {
              if (dataSnapshot.connectionState == ConnectionState.waiting) {
                return const Center(
                  child: CircularProgressIndicator(),
                );
              } else {
                return ListView.builder(
                    itemCount: (dataSnapshot.data as List).length,
                    itemBuilder: (ctx, index) {
                      final note = (dataSnapshot.data as List)[index];
                      return NoteTileWidget(
                        body: note[index].body,
                        title: note[index].title,
                        timeAdded:
                            ('${note[index].timeAdded.day}/${note[index].timeAdded.month}/${note[index].timeAdded.year}'),
                        id: note[index].id,
                      );
                    });
              }
            });
      }
    }
    

    【讨论】:

    • 未来的构建器给出了一个不同的错误,它说:null 不是 List 的子类型
    猜你喜欢
    • 2020-09-20
    • 2020-04-07
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    相关资源
    最近更新 更多