简体   繁体   English

未来<list<note> &gt; 不是 List 类型的子类型<note></note></list<note>

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

I am getting the data from sqlite and trying to set it in a List view but I am getting error:我从 sqlite 获取数据并尝试在列表视图中设置它,但出现错误:

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

Here is my sqlite query function:这是我的 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>;

} }

And here is where am calling readAllNotes():这里是调用 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,
      );
    });

} } } }

You have to await for the result.你必须等待结果。 Change this line:更改此行:

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

to

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.

相关问题 输入'未来<dynamic> ' 不是类型 'List 的子类型<application> ?</application></dynamic> - type 'Future<dynamic>' is not a subtype of type 'List<Application>?' 输入&#39;未来<List<Data> &gt;&#39; 不是 &#39;List 类型的子类型<Data> &#39; 在类型转换中 - type 'Future<List<Data>>' is not a subtype of type 'List<Data>' in type cast 颤振:输入“未来”<dynamic> &#39; 不是类型 &#39;Uint8List&#39; 的子类型 - Flutter: type 'Future<dynamic>' is not a subtype of type 'Uint8List' Dart Flutter Future Void _CastError(类型“Null”不是类型“List”的子类型<string?> ' 在类型转换中)错误</string?> - Dart Flutter Future Void _CastError (type 'Null' is not a subtype of type 'List<String?>' in type cast) Error Flutter Api 响应列表动态不是未来的子类型 - Flutter Api Response List dynamic is not subtype of future 无法从Android记事本应用程序的列表和数据库中删除记事本 - Can not delete the note from the list and from the database in my note app for Android 应用内部的Google Keep列表注释 - Google Keep list note from inside your app _TypeError(输入&#39;列表<dynamic> &#39; 不是类型 &#39;PlaceDetails&#39; 的子类型) - _TypeError (type 'List<dynamic>' is not a subtype of type 'PlaceDetails') “Null”类型不是“List”类型的子类型<Widget> &#39; - type 'Null' is not a subtype of type 'List<Widget>' 输入'列表<dynamic> ' 不是类型 'List 的子类型<expense> ? 在类型转换中</expense></dynamic> - type 'List<dynamic>' is not a subtype of type 'List<Expense>?' in type cast
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM