因此,我目前在Firebase中有一个数据库,其中包含我正在向用户显示的信息。作为管理员,我想更新信息。但是,当我尝试更新它时,会将新记录添加到Firebase实时数据库中。
这是显示实时数据库中所有记录的代码,以及将其重定向到更新页的编辑图标:
class _UpdateDestinationsState extends State<UpdateDestinations> {
final destdatabaseref = FirebaseDatabase.instance
.reference()
.child('Database')
.child('DestinationsandActivities')
.orderByChild("category")
.equalTo("Destination");
@override
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
iconTheme: IconThemeData(color: Colors.white), //add this line here
backgroundColor: Color(0xFF3d5a89),
elevation: 0,
centerTitle: true,
title: const Text('Update Destinations',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w500,
color: Colors.white)),
),
body: SafeArea(
child: FirebaseAnimatedList(
query: destdatabaseref,
itemBuilder: (BuildContext context, DataSnapshot snapshot,
Animation<double> animation, int index) {
return ListTile(
title: Text(snapshot.value['name']),
trailing: IconButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => UpdatePage(
name: snapshot.value['name'],
description: snapshot.value['description'],
id: snapshot.value['id'],
)));
},
icon: Icon(Icons.edit)),
);
})),
);
}
}这是更新页面的代码,您可以在这里编辑信息:
class UpdatePage extends StatefulWidget {
final String name;
final String description;
final String id;
UpdatePage(
{Key? key,
required this.name,
required this.description,
required this.id})
: super(key: key);
@override
_UpdatePageState createState() => _UpdatePageState();
}
class _UpdatePageState extends State<UpdatePage> {
final destdatabaseref = FirebaseDatabase.instance
.reference()
.child('Database')
.child('DestinationsandActivities');
@override
Widget build(BuildContext context) {
final nameController = TextEditingController();
final descriptionController = TextEditingController();
setState(() {
nameController.text = widget.name;
descriptionController.text = widget.description;
});
return Scaffold(
appBar: AppBar(
iconTheme: IconThemeData(color: Colors.white),
backgroundColor: Color(0xFF3d5a89),
elevation: 0,
centerTitle: true,
title: const Text('Update Destinations',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w500,
color: Colors.white)),
),
body: Form(
child: Padding(
padding: EdgeInsets.all(8),
child: Column(
children: [
TextFormField(
controller: nameController,
autofocus: false,
decoration: InputDecoration(
labelText: 'Name',
border: OutlineInputBorder(),
),
),
SizedBox(height: 10),
TextFormField(
controller: descriptionController,
autofocus: false,
decoration: InputDecoration(
labelText: 'Description',
border: OutlineInputBorder(),
),
),
TextButton(
onPressed: () {
updateData(widget.name, widget.description, widget.id);
Navigator.pop(context);
},
child: Text('Update')),
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('Cancel')),
],
),
),
));
}
void updateData(String name, String description, var key) {
Map<String, String> newvalue = {'name': name, 'description': description};
destdatabaseref.child(key).update(newvalue);
}
}使用此代码,单击update this按钮时,将这种类型的记录添加到实时数据库中:

我怎么才能解决这个问题?
发布于 2021-11-12 17:11:35
我终于发现了错误。updateData(widget.name, widget.description, widget.id);应该是:updateData(nameController.text, descriptionController.text, widget.id);。
发布于 2021-11-10 14:40:05
我觉得问题是拿到身份证。
实时将使用生成的密钥(id) --如果您传递空值,我以前从未使用过FirebaseAnimatedList,但是我看到了一个示例,其中他们使用snapshot.key访问密钥.
但是我确信它不是snapshot.value['id'],因为在您的结构中没有带有名称和装饰的id,并且它将作为对象返回,因此id将不是访问它的字段。
所以,首先尝试打印snapshot.value['id'],如果它包含id,然后跳过我的答案,如果它为null try snapshot.key,请查看快照是如何用FirebaseAnimatedList构造的,如果这也不起作用。
https://stackoverflow.com/questions/69912586
复制相似问题