Home > Blockchain >  Flutter Looking up a deactivated widget's ancestor is unsafe if adding to animated list
Flutter Looking up a deactivated widget's ancestor is unsafe if adding to animated list

Time:09-17

I am using a AnimatedList in my app. The problem is that when trying to insert an item I get this error:

Looking up a deactivated widget's ancestor is unsafe.

Here is my List:

child: AnimatedList(
                        padding: EdgeInsets.zero,
                        shrinkWrap: true,
                        key: listKey,
                        initialItemCount: month.memories.length,
                        itemBuilder: (context, index, animation) {
                          return slideIt(
                            context,
                            month.memories[index],
                            index,
                            animation,
                            month,
                          );
                        },
                      ),

Right now the List is in View 1 and the user pushes to View 2 where he adds the data on that View 2 I am calling a Callback Function inside dispose so I get notified if the view was popped. Here is the logic for that:

CupertinoScaffold.showCupertinoModalBottomSheet(
                      duration: Duration(
                        milliseconds: 350,
                      ),
                      expand: true,
                      context: context,
                      builder: (context) => AddMemoryPage(
                        memoryWasAdded: (addedMemory) {
                          if (addedMemory != null) {
                            _addMemory(context, addedMemory);
                          }
                        },
                      ),
                    );

and addMemory looks like this:

  _addMemory(BuildContext context, Memory memoryToAdd) {

month.memories.add(memoryToAdd);

AnimatedList.of(context).insertItem(
  month.memories.length - 1,
  duration: Duration(
    milliseconds: 500,
  ),
);

}

What am I doing wrong here? I also tried it with listKey.currentState but that also fails because. currentState is null. What is the way to go here?

Let me know if you need any more info!

CodePudding user response:

I fixed the problem by removing the callback and simply pass a value on popping the 2nd screen (pop(context, data) and then I can retrieve it like this:

onTapped: () {
                    CupertinoScaffold.showCupertinoModalBottomSheet(
                      duration: Duration(
                        milliseconds: 350,
                      ),
                      expand: true,
                      context: context,
                      builder: (context) => AddMemoryPage(),
                    ).then(
                      (memory) {
                        if (memory is Memory) {
                          _addMemory(context, memory);
                        }
                      },
                    );
                  },
  • Related