**我正在尝试将新任务插入数据库,但是当我返回主屏幕时,没有任何更新,我应该重新加载以查看更改,您知道为什么会发生吗?
我尝试了几件事,但问题仍然存在。如果您需要更多详细信息,请告诉我。谢谢! **
´import 'package:flutter/material.dart';
import 'package:todo_task/models/Task.dart';
import 'package:todo_task/utils/Helper.dart';
import 'package:todo_task/widgets/reusable_card.dart';
class TaskList extends StatefulWidget {
final String status;
const TaskList({this.status});
@override
_TaskListState createState() => _TaskListState();
}
class _TaskListState extends State<TaskList> {
final Helper helper = Helper();
@override
Widget build(BuildContext context) {
return FutureBuilder<List<Task>>(
future: helper.getTasks(widget.status),
builder: (BuildContext context, AsyncSnapshot<List<Task>> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.active:
case ConnectionState.waiting:
return Center(child: CircularProgressIndicator());
default:
if (snapshot.hasError)
return Center(child: Text('Error: ${snapshot.error}'));
else
return Column(
children: snapshot.data
.map((task) => ReusableCard(
task: task,
))
.toList(),
);
}
});
}
}
import 'package:flutter/material.dart';
import 'package:todo_task/widgets/custom_date.dart';
import 'package:todo_task/widgets/new_task.dart';
import 'package:todo_task/widgets/task_list.dart';
import '../constants.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(28.0, 20.0, 28.0, 20.0),
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
CustomDate(),
NewTask(),
],
),
Divider(),
SizedBox(height: 10),
Text(
'TODO TASKS',
style: kAppTitleTextStyle,
),
SizedBox(height: 15),
TaskList(status: 'uncompleted',),
Divider(),
SizedBox(height: 10),
Text(
'DONE TASKS',
style: kAppTitleTextStyle,
),
SizedBox(height: 15),
TaskList(status: 'completed',),
SizedBox(height: 15),
],
),
),
),
);
}
}´
答案 0 :(得分:0)
在更新数据库时,基本上是在更新设备中的文件,并且不会进行热重装。您应该做的是,在数据库操作完成后,更新获取任务的Future。
此外,请注意,最佳实践是通过以下方式保存在State的FutureBuilder中使用的Future实例:
setState((){
this.tasksFuture = helper.getTasks(widget.status);
});