如何在 Flutter 中每天重置事物?
How to reset things daily in Flutter?
我有一个待办事项列表,其中包含每天要做的事情,例如 'Drink Water' 等等。所以现在我希望当用户选中一个 To Do 时,第二天应该再次取消选中它。我该如何实施?
现在我的代码如下所示:
class ToDoScreen extends StatefulWidget {
const ToDoScreen({Key key}) : super(key: key);
@override
_ToDoScreenState createState() => _ToDoScreenState();
}
class _ToDoScreenState extends State<ToDoScreen> {
List toDos = ['Train Today', 'Drink Water'];
bool value = false;
void addToDo(String newToDo) {
setState(() {
toDos.add(newToDo);
});
Navigator.of(context).pop();
}
void newEntry() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: TextField(
onSubmitted: addToDo,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10)),
icon: Icon(Icons.text_snippet_outlined),
labelText: 'New To Do'),
),
);
});
}
正如 Yeasin Sheikh 在评论中所说,首先你需要将数据保存在某个地方,否则你没有任何东西可以重置。
我在评论中看到你想使用 Firebase。
所以我想您将在 Firebase Firestore 中存储数据。
在我看来,执行此类重置的最佳方法是使用 Firebase 函数 (docs)。
您可以将它们视为服务器端代码。从那里您可以定义一个函数来访问您的数据库并重置数据。
要每天触发一次该函数,您应该使用外部服务来调用该函数。
我发现这个问题可以帮助你:
在使用 firebase 时,您可以像 Gangemi 所说的那样使用 firebase 函数。
顺便说一句,我的方法将在 AppSite 上发生,您需要在每次启动时或显示数据之前检查主 class 中的重置功能。
if(DateTime.now().day != savedDate.day) reset();
您还可以添加过期日期时间并使用条件状态。
DateTime.now().add(Duration(days:1))
并使用
if(DateTime.now().day == savedDate.day) reset().
reset()
表示您正在迭代列表。在 firebase/storage.
这里
reset()
表示 delete data
来自存储
我有一个待办事项列表,其中包含每天要做的事情,例如 'Drink Water' 等等。所以现在我希望当用户选中一个 To Do 时,第二天应该再次取消选中它。我该如何实施?
现在我的代码如下所示:
class ToDoScreen extends StatefulWidget {
const ToDoScreen({Key key}) : super(key: key);
@override
_ToDoScreenState createState() => _ToDoScreenState();
}
class _ToDoScreenState extends State<ToDoScreen> {
List toDos = ['Train Today', 'Drink Water'];
bool value = false;
void addToDo(String newToDo) {
setState(() {
toDos.add(newToDo);
});
Navigator.of(context).pop();
}
void newEntry() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: TextField(
onSubmitted: addToDo,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10)),
icon: Icon(Icons.text_snippet_outlined),
labelText: 'New To Do'),
),
);
});
}
正如 Yeasin Sheikh 在评论中所说,首先你需要将数据保存在某个地方,否则你没有任何东西可以重置。
我在评论中看到你想使用 Firebase。
所以我想您将在 Firebase Firestore 中存储数据。
在我看来,执行此类重置的最佳方法是使用 Firebase 函数 (docs)。
您可以将它们视为服务器端代码。从那里您可以定义一个函数来访问您的数据库并重置数据。
要每天触发一次该函数,您应该使用外部服务来调用该函数。
我发现这个问题可以帮助你:
在使用 firebase 时,您可以像 Gangemi 所说的那样使用 firebase 函数。
顺便说一句,我的方法将在 AppSite 上发生,您需要在每次启动时或显示数据之前检查主 class 中的重置功能。
if(DateTime.now().day != savedDate.day) reset();
您还可以添加过期日期时间并使用条件状态。
DateTime.now().add(Duration(days:1))
并使用
if(DateTime.now().day == savedDate.day) reset().
reset()
表示您正在迭代列表。在 firebase/storage.
这里
reset()
表示 delete data
来自存储