Flutter 关闭颤振应用程序后如何保存数据?
我试着创建一个应用程序,你可以简单地添加一个新的待办事项。我的问题是,在添加了一个新的TODO之后,然后关闭我的应用程序,我的TODO列表是空的。在应用程序中保存数据的最佳方式是什么?有什么简单的解决方案吗?作为一个基本实现,我想说您应该使用共享首选项它使用键值来存储数据,而且使用起来非常简单。在您清除缓存卸载数据之前,数据将一直保留Flutter 关闭颤振应用程序后如何保存数据?,flutter,dart,state-management,Flutter,Dart,State Management,我试着创建一个应用程序,你可以简单地添加一个新的待办事项。我的问题是,在添加了一个新的TODO之后,然后关闭我的应用程序,我的TODO列表是空的。在应用程序中保存数据的最佳方式是什么?有什么简单的解决方案吗?作为一个基本实现,我想说您应该使用共享首选项它使用键值来存储数据,而且使用起来非常简单。在您清除缓存卸载数据之前,数据将一直保留 请查看我创建的待办事项列表代码示例 import 'package:flutter/material.dart'; import 'package:shared_
请查看我创建的待办事项列表代码示例
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
child: TODOApp(),
),
),
);
}
}
class TODOApp extends StatefulWidget {
TODOApp({Key key}) : super(key: key);
@override
_TODOAppState createState() => _TODOAppState();
}
class _TODOAppState extends State<TODOApp> {
TextEditingController _todoController = TextEditingController();
List<String> todoList = List();
bool _isLoading = false;
_saveList(list) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setStringList("key", list);
return true;
}
_getSavedList() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (prefs.getStringList("key") != null)
todoList = prefs.getStringList("key");
setState(() {});
}
@override
void initState() {
super.initState();
setState(() {
_isLoading = true;
});
_getSavedList();
setState(() {
_isLoading = false;
});
}
Future<bool> _onWillPop() async {
return (await showDialog(
context: context,
builder: (context) => new AlertDialog(
title: new Text('Are you sure?'),
content: new Text('Do you want to exit an App'),
actions: <Widget>[
new FlatButton(
onPressed: () => Navigator.of(context).pop(false),
child: new Text('No'),
),
new FlatButton(
onPressed: () {
_saveList(todoList);
Navigator.of(context).pop(true);
},
child: new Text('Yes'),
),
],
),
)) ??
false;
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: _onWillPop,
child: SafeArea(
child: _isLoading
? Center(
child: CircularProgressIndicator(),
)
: Container(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
decoration: InputDecoration(
hintText: 'Add Items',
),
controller: _todoController,
onSubmitted: (text) {
// do what you want with the text
todoList.add(_todoController.text);
_todoController.clear();
setState(() {});
},
),
),
todoList == null
? Container(
child: Center(
child: Text('Please Add the items'),
),
)
: ListView.builder(
shrinkWrap: true,
itemCount: todoList.length,
itemBuilder: (BuildContext context, int index) {
return Card(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(todoList[index]),
),
);
},
),
],
),
),
),
);
}
}
导入“包装:颤振/材料.省道”;
导入“package:shared_preferences/shared_preferences.dart”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
constmyapp({Key}):超级(Key:Key);
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
主体:容器(
子项:TODOApp(),
),
),
);
}
}
类TODOApp扩展了StatefulWidget{
TODOApp({Key}):super(Key:Key);
@凌驾
_TODOAppState createState()=>\u TODOAppState();
}
类_TODOAppState扩展了状态{
TextEditingController_todoController=TextEditingController();
List-todoList=List();
bool_isLoading=false;
_存储列表(列表)异步{
SharedReferences prefs=等待SharedReferences.getInstance();
首选设置字符串(“键”,列表);
返回true;
}
_getSavedList()异步{
SharedReferences prefs=等待SharedReferences.getInstance();
if(prefs.getStringList(“key”)!=null)
todoList=prefs.getStringList(“键”);
setState((){});
}
@凌驾
void initState(){
super.initState();
设置状态(){
_isLoading=true;
});
_getSavedList();
设置状态(){
_isLoading=false;
});
}
Future\u onWillPop()异步{
返回(等待显示)对话框(
上下文:上下文,
生成器:(上下文)=>新建警报对话框(
标题:新文本(“你确定吗?”),
内容:新文本(“是否要退出应用程序”),
行动:[
新扁平按钮(
onPressed:()=>Navigator.of(context.pop)(false),
子项:新文本(“否”),
),
新扁平按钮(
已按下:(){
_存储列表(todoList);
Navigator.of(context.pop)(true);
},
子项:新文本(“是”),
),
],
),
)) ??
虚假的;
}
@凌驾
小部件构建(构建上下文){
返回式示波器(
onWillPop:_onWillPop,
儿童:安全区(
子项:_正在加载
?中心(
子对象:CircularProgressIndicator(),
)
:容器(
子:列(
儿童:[
填充物(
填充:常数边集全部(8.0),
孩子:TextField(
装饰:输入装饰(
hintText:“添加项目”,
),
控制器:\u todoController,
提交:(文本){
//对文本做你想做的
添加(\u todoController.text);
_todoController.clear();
setState((){});
},
),
),
todoList==null
?容器(
儿童:中心(
子项:文本(“请添加项”),
),
)
:ListView.builder(
收缩膜:对,
itemCount:todoList.length,
itemBuilder:(构建上下文,int索引){
回程卡(
孩子:填充(
填充:常数边集全部(8.0),
子项:文本(todoList[索引]),
),
);
},
),
],
),
),
),
);
}
}
让我知道它是否有效你好,谢谢你的回答。我试过了,效果很好。但是,没有其他解决方案如何在没有共享首选项的情况下做到这一点?共享首选项基本上适用于轻量级的东西,如果您在更大的平台上使用本地数据数据库,则首选。您知道如何保存列表吗?只有使用prefs.setStringList()保存StringList的可能性可能是不可能的,我还没有检查,但目前正在忙着工作,会在短时间内通知您。在此之前,您可以检查此链接: