DolDurma
DolDurma

Reputation: 17303

using Provider in nested part of widgets and getting dispose() error

I'm trying to using Provider to nested my screen page as TabBarView. when i try to use

this provider as: Provider.of(context).getSingleUser() into parent screen and one page on TabBarView in this screen together i get error:

At this point the state of the widget's element tree is no longer stable. E/flutter (21506): To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling inheritFromWidgetOfExactType() in the widget's didChangeDependencies() method.

in main function i defined this providers:

...  
MultiProvider(providers: [
  Provider(builder: (_) => database),
  Provider(builder: (_) => database.userTableDao),
  Provider(builder: (_) => settings),
] ...  ,

and into register_navigator.dart i have TabBarView with two children

FutureBuilder(
    future: Provider.of<UserTableDao>(context).getSingleUser(),
    builder: (context, snapshot) {
      if (snapshot.connectionState == ConnectionState.done) {
        UserTableData user = snapshot.data;
        ...
        TabBarView( ... ),
      }

      ...
}),

and then on child of this TabBarView i used

FutureBuilder(
    future: Provider.of<UserTableDao>(context).getSingleUser(),

again

WHEN I REMOVE PROVIDER IMPLEMENTATION FROM PARENT OF TabBarView THAT CAUSE OF REOLVING PROBLEM

Upvotes: 3

Views: 2558

Answers (2)

haroldolivieri
haroldolivieri

Reputation: 2283

Looks like the problem you're facing is related with a no longer valid context and according to provider documentation :

Alternatively instead of using Provider.of, we can use Consumer and Selector. These can be useful for performance optimizations or when it is difficult to obtain a BuildContext descendant of the provider

So, wrap your FutureBuilder inside a Consumer instead of using Provider.of:

Consumer<UserTableDao>(
  builder: (_, dao, __) => 
    FutureBuilder(
      future: dao.getSingleUser(),
      builder: (context, snapshot) {
    ...

Upvotes: 2

Filled Stacks
Filled Stacks

Reputation: 4346

If you're providing a value instead of a newly constructed Provider type, then consider using the Provider.value constructor. Especially if you want your value to not be disposed after one of the Provided instances goes out of scope.

Upvotes: 2

Related Questions