Sqlite 颤振:显示Sqflite中的所有项目时出错

Sqlite 颤振:显示Sqflite中的所有项目时出错,sqlite,flutter,dart,Sqlite,Flutter,Dart,我已经在颤振项目中创建了一个导航到notes\u page.dart的按钮。“注释”页面.dart包含使用sqflite包的注释数据库。当我第一次点击按钮时(在我运行项目之后),它正常运行并显示我的所有笔记。但是,当我回到主页并重新打开notes\u page.dart时,我的笔记看不到,屏幕上只显示CircularProgressIndicator() 这是我的notes\u页面。dartcode: class NotesPage extends StatefulWidget { @ove

我已经在颤振项目中创建了一个导航到
notes\u page.dart
的按钮。“注释”页面.dart包含使用sqflite包的注释数据库。当我第一次点击按钮时(在我运行项目之后),它正常运行并显示我的所有笔记。但是,当我回到主页并重新打开
notes\u page.dart
时,我的笔记看不到,屏幕上只显示
CircularProgressIndicator()

这是我的
notes\u页面。dart
code:

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(
        backgroundColor: Colors.teal,
        appBar: AppBar(
          title: Text(
            'Cie mau curhat ya :)',
            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(),
        ),
        floatingActionButton: FloatingActionButton(
          backgroundColor: Colors.black,
          child: Icon(Icons.add),
          onPressed: () async {
            await Navigator.of(context).push(
              MaterialPageRoute(builder: (context) => AddEditNotePage()),
            );

            refreshNotes();
          },
        ),
      );

  Widget buildNotes() => ListView.builder(
        padding: EdgeInsets.all(8),
        itemCount: notes.length,
        itemBuilder: (context, index) {
          final note = notes[index];

          return Container(
            color: Colors.transparent,
            child: GestureDetector(
              onTap: () async {
                await Navigator.of(context).push(MaterialPageRoute(
                  builder: (context) => NoteDetailPage(noteId: note.id!),
                ));

                refreshNotes();
              },
              child: NoteCardWidget(note: note, index: index),
            ),
          );
        },
      );
}
class NotesPage扩展StatefulWidget{
@凌驾
_NotesPageState createState()=>\u NotesPageState();
}
类_notespatate扩展状态{
迟交清单说明;
bool isLoading=false;
@凌驾
void initState(){
super.initState();
刷新注释();
}
@凌驾
无效处置(){
NotesDatabase.instance.close();
super.dispose();
}
Future refreshNotes()异步{
设置状态(()=>isLoading=true);
this.notes=wait NotesDatabase.instance.readAllNotes();
设置状态(()=>isLoading=false);
}
@凌驾
小部件构建(构建上下文)=>Scaffold(
背景颜色:Colors.teal,
appBar:appBar(
标题:正文(
“Cie mau curhat ya:)”,
样式:TextStyle(字体大小:24),
),
操作:[图标(Icons.search),大小框(宽度:12)],
),
正文:中(
孩子:孤岛
?循环压缩机指示器()
:notes.i空
?文本(
“没有注释”,
样式:TextStyle(颜色:Colors.white,字体大小:24),
)
:buildNotes(),
),
浮动操作按钮:浮动操作按钮(
背景颜色:Colors.black,
子:图标(Icons.add),
onPressed:()异步{
等待导航器。的(上下文)。推送(
MaterialPageRoute(生成器:(上下文)=>AddEditNotePage()),
);
刷新注释();
},
),
);
Widget buildNotes()=>ListView.builder(
填充:边缘设置。全部(8),
itemCount:notes.length,
itemBuilder:(上下文,索引){
最终注释=注释[索引];
返回容器(
颜色:颜色。透明,
儿童:手势检测器(
onTap:()异步{
等待(上下文)的导航器(
生成器:(上下文)=>NoteDetailPage(noteId:note.id!),
));
刷新注释();
},
子:NoteCardWidget(注意:注意,索引:索引),
),
);
},
);
}

请在这个问题中分享您的
注释页面。dart
。我想当你这样做的时候,志愿者可以帮助你。因为不是每个人都想加入这个链接,也不是每个想加入的人都能回答你的问题。@GilangPratama好的,谢谢:)
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(
        backgroundColor: Colors.teal,
        appBar: AppBar(
          title: Text(
            'Cie mau curhat ya :)',
            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(),
        ),
        floatingActionButton: FloatingActionButton(
          backgroundColor: Colors.black,
          child: Icon(Icons.add),
          onPressed: () async {
            await Navigator.of(context).push(
              MaterialPageRoute(builder: (context) => AddEditNotePage()),
            );

            refreshNotes();
          },
        ),
      );

  Widget buildNotes() => ListView.builder(
        padding: EdgeInsets.all(8),
        itemCount: notes.length,
        itemBuilder: (context, index) {
          final note = notes[index];

          return Container(
            color: Colors.transparent,
            child: GestureDetector(
              onTap: () async {
                await Navigator.of(context).push(MaterialPageRoute(
                  builder: (context) => NoteDetailPage(noteId: note.id!),
                ));

                refreshNotes();
              },
              child: NoteCardWidget(note: note, index: index),
            ),
          );
        },
      );
}