gpt4 book ai didi

android - 在 Flutter 应用程序中持久化数据

转载 作者:行者123 更新时间:2023-11-28 23:22:04 25 4
gpt4 key购买 nike

我正在构建一个应用程序,在其中,我有一个列表中的人名,我可以从中添加/删除等。问题是当我关闭应用程序时,这个列表不会被保存,这很不方便。

我听说您可以使用共享首选项来保存像这样的简单对象,而不会像使用 SQLite 和 json 那样复杂化。

所以我想知道持久保存和加载这些数据的建议方法是什么。

提前致谢,祝您有美好的一天:)

代码如下:

import 'package:flutter/material.dart';
import 'package:zakif_yomi3/NewPerson.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.purple,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

final List<String> people = [];

void _addNewPerson(String name) {
setState(() {
people.add(name);
});
}

void _startAddNewPerson(BuildContext ctx) {
showModalBottomSheet(
context: ctx,
builder: (_) {
return GestureDetector(
onTap: () {},
child: NewPerson(_addNewPerson),
behavior: HitTestBehavior.opaque,
);
},
);
}
void _deletePerson(int value ) {

setState(() {
people.removeAt(value);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text(
'People',
style: TextStyle(fontSize: 30),
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.add),
onPressed: () => _startAddNewPerson(context),
)
],
),
body: ListView.builder(
itemCount: this.people.length,
itemBuilder: (context, value) {
return Card(
color: Colors.amberAccent[200],
elevation: 3,
child: Container(
child: ListTile(
leading: Text(value.toString()),
title: Text(
people[value],
),
trailing: IconButton(
icon: Icon(Icons.delete),
onPressed: () {
_deletePerson(value);
},
),
),
),
);
},
),
);
}
}

还有 NewPerson 对象:

import 'package:flutter/material.dart';


class NewPerson extends StatefulWidget {
final Function addTx;

NewPerson(this.addTx);

@override
_NewPersonState createState() => _NewPersonState();
}

class _NewPersonState extends State<NewPerson> {
final _nameController = TextEditingController();



void _submitData() {

final name = _nameController.text;


widget.addTx(
name
);

Navigator.of(context).pop();
}



@override
Widget build(BuildContext context) {
return Card(
elevation: 5,
child: Container(
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
TextField(
decoration: InputDecoration(labelText: 'Name'),
controller: _nameController,
onSubmitted: (_) => _submitData(),

),


RaisedButton(
child: Text('Add Person'),
color: Theme.of(context).primaryColor,
textColor: Theme.of(context).textTheme.button.color,
onPressed: _submitData,
),
],
),
),
);
}
}

最佳答案

您可以使用此函数从共享首选项中保存和加载数据。

here 获取 SharedPreferences| .

将数据保存到 SharedPreferences,在向列表添加或删除新元素后调用。

_persistData() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
await preferences.setStringList("persons", _people);
}

从SharedPreferences中加载数据,通常在initState中调用。

_loadData() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
setState(() {
_people = preferences.getStringList("persons");
});
}

关于android - 在 Flutter 应用程序中持久化数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59499200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com