如何解决按回 Flutter 后列表数据消失
每当我将一个项目添加到我的最少时,它就会在当前状态下添加到列表中,但是当我在弹出上下文后重新打开时,该项目不再存在 *如何永久拥有列表中的项目 *enter image description hereenter image description hereenter image description hereenter image description hereenter image description here
class Prabesh extends StatefulWidget {
@override
_PrabeshState createState() => _PrabeshState();
}
class _PrabeshState extends State<Prabesh> {
final List users = [
'Ram','Shyam','Hari','Paudel','Pandey','Sashwat','Kismat',];
// ondlt(Key key) {
// for (int i = 0; i <= users.length; i++) {
// var a = users.elementAt(i);
// if (key == a.key) {
// users.removeAt(i);
// }
// }
// }
void onClk() {
myDialog(BuildContext,context);
}
final myUser = TextEditingController();
myDialog(BuildContext,context) {
showDialog(
barrierDismissible: false,context: context,builder: (context) {
return AlertDialog(
title: Text('Add a user'),content: TextFormField(
controller: myUser,maxLength: 20,decoration: InputDecoration(
hintText: 'Enter Username',),actions: [
ElevatedButton(
onPressed: () => Navigator.pop(context),child: Text('Cancel')),ElevatedButton(
onPressed: () {
setState(() {
if (myUser.text.isEmpty) {
return Navigator.pop(context);
} else {
users.add(myUser.text);
return Navigator.pop(context);
}
});
},child: Text('Add'),],);
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Prabesh'),leading: BackButton(
onPressed: () => Navigator.pop(context),body: ListView.builder(
itemCount: users.length,itemBuilder: (context,index) {
return Card(
elevation: 10,child: ListTile(
// key: Key(users[index]),//onLongPress: () => ondlt(key),onTap: () {},title: Text(users[index]),trailing: Icon(Icons.menu),leading: CircleAvatar(
child: Text(users[index].substring(0,1)),);
},floatingActionButton: FloatingActionButton(
onPressed: () => onClk(),child: Icon(Icons.add),);
}
}
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。