Search code examples
flutterstream-builderflutter-animatedlist

Itembuilder doesn't execute in streambuilder, when updating data in firestore


When I launch the app for the first time it loads in all the items into the animated list. But when If I add an item to firestore, the streambuilder realizes something has happend. BUT it doesn't execute the itembuilder so no new items will show up. However if I relaunches the app, the new items will be loaded. If I would just change a name on an existing item on the firestore database, it will rebuild with no problem.

When I update an item. I have to get the map of tasks and replace it with a new copy that has the item and post/update that to firestore.

I have tried everything I can think off, doesn't get why this happens. I have printed in the itembuilder and can see that it doesn't execute on an update.

Widget _buildTasksList() {

    return new Expanded(
      child: new StreamBuilder(
      stream: Firestore.instance
          .collection("lists")
          .document(tasklist.postID)
          .snapshots(),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        if (!snapshot.hasData) {
          return new Text("Loading");
        }
        var listdata = snapshot.data;
        tasklist.tasks = listdata['tasks'];
        tasklist.owners = listdata['owners'];
        tasklist.sorter = listdata['sorter'];
        return new AnimatedList(
          initialItemCount: convertToTaskList(listdata['tasks']).length,
          key: _listKey,
          itemBuilder: (context, index, animation) {
            return new TaskRow(
              task: this.listModel[index],
              animation: animation,
              listModel: this.listModel,
              tasklist: tasklist,
              onChange: () => _onChange(this.listModel[index]),
            );
          },
        );
      },
    )
    );
  }

Solution

  • Got it working by resetting my _listKey, added this before animated list. _listKey = null; _listKey = new GlobalKey();