簡體   English   中英

未來<list<note> &gt; 不是 List 類型的子類型<note></note></list<note>

[英]Future<List<Note>> is not a subtype of type List<Note>

我從 sqlite 獲取數據並嘗試在列表視圖中設置它,但出現錯誤:

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

這是我的 sqlite 查詢 function:

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> {
  // late Future<List<Note>> note;

  @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,
      );
    });

} }

你必須等待結果。 更改此行:

List<Note> note = NotesDataBase.instance.readAllNotes() as List<Note>;

List<Note> note = await NotesDataBase.instance.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) { 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, ); }); } }); } }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM