Home > database >  Flutter: update Firebase snapshot data
Flutter: update Firebase snapshot data

Time:09-12

I have these 3 pages:

  • A: with a list of items. This list is a stream coming from Firebase
  • B: Item info
  • C: Item update

In B I show whatever I send from A. Same for C. When I modify the element this does the update in 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:

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

I need to do widget.data['item']['title'] = 'New title';? Or it possible to modify the whole item like widget.data['item'].data() = newItem; (this doesn't work)?

In page 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);

CodePudding user response:

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. So whenever I need to get the id I'll use widget.data['item'].id() and itemCopy for the info

  • Related