Android studio flatter SQFlite:在使用数据库内容更新listview时遇到问题
我确实设法通过listview显示了数据库内容。然而,它没有做得很好。只要我运行模拟器,数据库中存储的内容就会出现,但不会出现。它还显示数据库是空的,尽管它显然不是空的。但是,一旦我向数据库中添加了一个新项目并单击“保存”,所有内容都会显示出来(除了上次添加的项目,它会在下次更新时显示)。我不知道如何解决这个问题,我尝试将_listtoos()放在任何地方,看看它是否在小部件之前执行,但没有任何改进我想问题出在ListDisplay类中,因此您可以跳过其余部分,为了方便起见,我只需要完整的代码Android studio flatter SQFlite:在使用数据库内容更新listview时遇到问题,android-studio,flutter,dart,local-storage,sqflite,Android Studio,Flutter,Dart,Local Storage,Sqflite,我确实设法通过listview显示了数据库内容。然而,它没有做得很好。只要我运行模拟器,数据库中存储的内容就会出现,但不会出现。它还显示数据库是空的,尽管它显然不是空的。但是,一旦我向数据库中添加了一个新项目并单击“保存”,所有内容都会显示出来(除了上次添加的项目,它会在下次更新时显示)。我不知道如何解决这个问题,我尝试将_listtoos()放在任何地方,看看它是否在小部件之前执行,但没有任何改进我想问题出在ListDisplay类中,因此您可以跳过其余部分,为了方便起见,我只需要完整的代码
import 'package:flutter/material.dart';
import 'model/todo_model.dart';
import 'model/todo.dart';
class ListGradesPage extends StatefulWidget {
final String title;
ListGradesPage({this.title, Key key}) : super(key: key);
_ListGradesPageState createState() => _ListGradesPageState();
}
var _todoItem;
var _lastInsertedId = 0;
final _model = TodoModel();
Future<void> _deleteTodo() async {
_model.deleteAllTodo();
}
class _ListGradesPageState extends State<ListGradesPage> {
//final _model = TodoModel();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
actions: <Widget>[
IconButton(
icon: Icon(Icons.edit),
onPressed: () {
print('editing...');
//_listTodos();
}),
IconButton(
icon: Icon(Icons.delete),
onPressed: () {
print('deleting all...');
setState(() {
_deleteTodo();
});
}),
],
),
body: ListDisplayPage(),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => AddGrade()),
);
},
child: Icon(Icons.add),
backgroundColor: Colors.blue,
),
);
}
}
class ListDisplayPage extends StatefulWidget {
ListDisplay createState() => ListDisplay();
}
class ListDisplay extends State<ListDisplayPage> {
int _selectedIndex;
List<Todo> todos = new List<Todo>();
Future<void> _listTodos() async {
todos = await _model.getAllTodos();
if (todos.isEmpty) {
print('it is empty-2.');
} else {
print("not empty-2");
}
print('To Dos:');
for (Todo todo in todos) {
print(todo);
}
}
_onSelected(int index) {
_selectedIndex = index;
}
@override
void initState() {
super.initState();
_listTodos();
// _listTodos() is not calling the function above for some reason, but executes everything below it
// In the database there are items stored however it says the databse is empty...
// until i add a new item and everything shows up except the item i recently added
if (todos.isEmpty) {
print('To Dos:');
for (Todo todo in todos) {
print(todo);
}
print('it is empty.');
} else {
print("not empty");
}
}
@override
Widget build(BuildContext context) {
//Color clr = Colors.transparent;
return todos.isEmpty
? Center(
child: Text(
'Nothing to show! Is it empty? ' + (todos.isEmpty).toString()))
: ListView.builder(
itemCount: todos.length,
itemBuilder: (context, index) {
return Card(
// <-- Card widget
child: Container(
color: _selectedIndex != null && _selectedIndex == index
? Colors.lightBlueAccent
: Colors.transparent,
child: ListTile(
leading: GestureDetector(
onTap: () {
setState(() {
_onSelected(index);
print(index);
});
},
child: Container(
width: MediaQuery.of(context).size.width / 2,
child:
Text(todos[index].sid + '\n' + todos[index].grade),
),
),
),
),
);
},
);
}
}
class AddGrade extends StatelessWidget {
String _sid, _grade;
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Add Grade"),
),
body: Form(
key: _formKey,
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(15.0),
child: TextFormField(
decoration: const InputDecoration(
hintText: 'Student ID',
labelText: 'SID',
),
onSaved: (String value) {
print('Saving SID $value');
_sid = value.toString();
},
),
),
Padding(
padding: const EdgeInsets.all(15.0),
child: TextFormField(
decoration: const InputDecoration(
hintText: 'Student Grade',
labelText: 'Grade',
),
onSaved: (String value) {
print('Saving Grade $value');
_grade = value.toString();
},
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
_formKey.currentState.save();
_addTodo();
//_listTodos();
Navigator.pop(context);
},
child: Icon(Icons.save),
backgroundColor: Colors.blue,
),
);
}
Future<void> _addTodo() async {
Todo newTodo = Todo(sid: _sid, grade: _grade);
_lastInsertedId = await _model.insertTodo(newTodo);
}
}
emulator的屏幕截图,添加新学生之前和添加新学生之后(在我再次添加另一个学生之前,最近的一个也不会显示)
如果我没有遵循指导原则,以前从未在这里发布过问题,我深表歉意。你知道吗?我建议将您的ListView.builder
包装到未来的生成器中,并将\u loadTodos
函数作为未来的生成器传递。然后可以删除初始化TODO的所有initState代码
通常,我发现在initState
中执行异步工作并不总是有效的。UI已经生成,不知道在异步工作完成时重建。另一个选项可能只是在异步函数完成TODO后调用setState
。我认为FutureBuilder是更好的方法
如果你需要额外的帮助,请告诉我。我可能会给你写一些
Restarted application in 1,607ms.
I/flutter (14196): To Dos:
I/flutter (14196): it is empty.
I/flutter (14196): not empty-2
I/flutter (14196): To Dos:
I/flutter (14196): Todo{id: 1, sid: q, grade: qq}
I/flutter (14196): Todo{id: 2, sid: w, grade: ww}
I/flutter (14196): Todo{id: 3, sid: e, grade: ee}
I/flutter (14196): Todo{id: 4, sid: r, grade: }
I/flutter (14196): Todo{id: 5, sid: hh, grade: kk}