首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法更新实时数据库中的数据

无法更新实时数据库中的数据
EN

Stack Overflow用户
提问于 2021-11-10 11:34:45
回答 2查看 280关注 0票数 0

因此,我目前在Firebase中有一个数据库,其中包含我正在向用户显示的信息。作为管理员,我想更新信息。但是,当我尝试更新它时,会将新记录添加到Firebase实时数据库中。

这是显示实时数据库中所有记录的代码,以及将其重定向到更新页的编辑图标:

代码语言:javascript
运行
复制
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)),
                );
              })),
    );
  }
}

这是更新页面的代码,您可以在这里编辑信息:

代码语言:javascript
运行
复制
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按钮时,将这种类型的记录添加到实时数据库中:

我怎么才能解决这个问题?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-12 17:11:35

我终于发现了错误。updateData(widget.name, widget.description, widget.id);应该是:updateData(nameController.text, descriptionController.text, widget.id);

票数 0
EN

Stack Overflow用户

发布于 2021-11-10 14:40:05

我觉得问题是拿到身份证。

实时将使用生成的密钥(id) --如果您传递空值,我以前从未使用过FirebaseAnimatedList,但是我看到了一个示例,其中他们使用snapshot.key访问密钥.

但是我确信它不是snapshot.value['id'],因为在您的结构中没有带有名称和装饰的id,并且它将作为对象返回,因此id将不是访问它的字段。

所以,首先尝试打印snapshot.value['id'],如果它包含id,然后跳过我的答案,如果它为null try snapshot.key,请查看快照是如何用FirebaseAnimatedList构造的,如果这也不起作用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69912586

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档