1

I am getting this error in my code -> Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.

I am using riverpod in my flutter project and doing some api call and the data which i am getting from api adding it to respective object through StateNotifierProvider and i am doing it in init state of the code.

When i swtich my screen in bottom navigation bar it is throwing this error.

Thanks in advance !!!

@override
void initState() {
checkPermission();

WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
  ref.read(adminRepositoryProvider).getAllBooking().then((value) {
    ref.read(bookingListProvider.notifier).changeData(value);
  });
});

super.initState();
 }

Whenever i switch my screen from one screen to another through bottom navigation bar it throws this error

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe. E/flutter (10366): At this point the state of the widget's element tree is no longer stable. E/flutter (10366): To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.

  • 1
    From what I can say you may wanna reconsider your app architecture because calling an API from the widget is not a good practice. they should be done from the repository and your widget should only be reading the data collected by your repository. and another thing is the `WidgetBinding.instance` method is being applied inside the build method not from the initState because that method is called after your widget is rebuilt. – john Oct 06 '22 at 13:15

0 Answers0