简体   繁体   English

Flutter:更新 Firebase 快照数据

[英]Flutter: update Firebase snapshot data

I have these 3 pages:我有这 3 页:

  • A: with a list of items. - 答:与项目清单。 This list is a stream coming from Firebase此列表是来自 Firebase 的 stream
  • B: Item info B:物品信息
  • C: Item update C:项目更新

In BI show whatever I send from A. Same for C.在 BI 中显示我从 A 发送的任何内容。对于 C 也是如此。 When I modify the element this does the update in Firebase.当我修改元素时,这会更新 Firebase。 Until here it's ok.到这里为止还好。

When it updates the item we move back again to B and here I need to update the snapshot data, because in this page we have something like this:当它更新项目时,我们再次移回 B,在这里我需要更新快照数据,因为在这个页面中我们有这样的内容:

Text(widget.data['item']['title']);

I need to do widget.data['item']['title'] = 'New title';我需要做widget.data['item']['title'] = 'New title'; ? ? Or it possible to modify the whole item like widget.data['item'].data() = newItem;或者可以修改整个项目,如widget.data['item'].data() = newItem; (this doesn't work)? (这不起作用)?

In page B:在 B 页中:

await Navigator.of(context)
        .pushNamed(
      "/new-item", // page C
      arguments: widget.data["item"], // this is the snapshot
    )
        .then((dynamic updatedItem) async { // this is the map
      if (lease != null) {
        setState(() {
          // widget.data["item"] = updatedItem; // this is the main problem. First contains a snapshot and second is the map
        });
        await firebaseProfile.updateLease(context, item);
      }
 });

/// ///

await db
        .collection('items')
        .doc(item.id) // since I get the map this doesn't work
        .update(item);

In resume, this is not possible, I couldn't find any way to update the snapshot so I had to change the logic.在简历中,这是不可能的,我找不到任何更新快照的方法,所以我不得不更改逻辑。

This is a different approach I have used:这是我使用的另一种方法:

 void initState() {
    itemCopy = widget.data['item'].data();
    super.initState();
  }

and keep the setState, in this case updating itemCopy instead.并保持 setState,在这种情况下更新itemCopy So whenever I need to get the id I'll use widget.data['item'].id() and itemCopy for the info因此,每当我需要获取 id 时,我都会使用widget.data['item'].id()itemCopy获取信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM