从使用SQFLITE创建的List中获取单个值



我想出了一个应用程序,从用户获取标题和描述作为输入,并使用sqflite将其存储在本地。每当用户按下保存按钮时,我应该能够在卡片小部件中仅创建标题列表或仅创建描述列表或两者。我累了这个子:Text(notes[NoteFields.description]),在buildNotes方法中,但我无法检索描述列表,但我获得类型'String'不是'index'类型'int'的子类型

class NotesPage extends StatefulWidget {
@override
_NotesPageState createState() => _NotesPageState();
}
class _NotesPageState extends State<NotesPage> {
late List<Note> notes;
bool isLoading = false;
@override
void initState() {
super.initState();
refreshNotes();
}
@override
void dispose() {
NotesDatabase.instance.close();
super.dispose();
}
Future refreshNotes() async {
setState(() => isLoading = true);
this.notes = await NotesDatabase.instance.readAllNotes();
setState(() => isLoading = false);
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: Text(
'Notes',
style: TextStyle(fontSize: 24),
),
actions: [Icon(Icons.search), SizedBox(width: 12)],
),
body: Center(
child: isLoading
? CircularProgressIndicator()
: notes.isEmpty
? Text(
'No Notes',
style: TextStyle(color: Colors.white, fontSize: 24),
)
: buildNotes(notes),
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.black,
child: Icon(Icons.add),
onPressed: () async {
await Navigator.of(context).push(
MaterialPageRoute(builder: (context) => AddEditNotePage()),
);
refreshNotes();
},
),
);
Widget buildNotes(notes) =>
Card(

child: Text(notes[NoteFields.description]),
);

}

notes是一个列表,使用listview builder小部件来呈现列表对象

ListView.builder(
itemCount: notes.length,
itemBuilder: (context, index) {
Card(
child: Text(notes[index][NoteFields.description]),
);
});

或使用整数索引来获取特定的值,如

Card(
child: Text(notes[0][NoteFields.description]),
);

最新更新