0

I am using provider: ^6.0.5 for state management for my flutter desktop app.

This is my Loader provider:

A simple class have 2 methods that will toggle the loaderState boolean value.

class LoaderProvider extends ChangeNotifier {
  bool loaderState = false;
  void showLoader() {
    loaderState = true;
    notifyListeners();
  }

  void hideLoader() {
    loaderState = false;
    notifyListeners();
  }
}

How I am registering this was :

 runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(create: ((context) {
          return LoaderProvider();
        }))
      ],
      child: MaterialApp(...))

Parent widget

Consumer<LoaderProvider>(
      builder: ((context, loaderProviderModel, child) {
      if (loaderProviderModel.loaderState) {
           print("Loader showww");
           return const LoadingIndicator();
       } else {
           print("Loader Off");
           return Container();
       }
              }))

If I am calling by the below code in another stateful widget:

    Provider.of<LoaderProvider>(context, listen: false).showLoader();

    Provider.of<LoaderProvider>(context, listen: false).hideLoader();

It is actually triggering but the UI is not updating

i.e) The printing statements "Loader show" and "Loader off" is coming.

Note : For normal case it is working but If I have any Future builder in my another stateful widget, That time it is not working.

Am I missing anything?

Thiru
  • 31
  • 6

3 Answers3

0

In cases like this I try to give my 2 widgets(LoadingIndicator() and Container() in Consumer) keys so the tree understands that one is replaced with another, so create 2 global key like :

var containerKey = GlobalKey();
var indicatorKey = GlobalKey();

and assign them to the widget

  • Can you please elaborate your answer, What are the two widgets you meant – Thiru Jul 23 '23 at 13:05
  • const LoadingIndicator() and Container() . the 2 widget you are using in your consumer – hasan foraty Jul 23 '23 at 13:10
  • Tried but it is not working, The thing is it is perfectly working in normal scenario, But if I am using any stateful widget that contains FutureBuilder and from that If I am triggering it is not updating the UI – Thiru Jul 23 '23 at 13:21
0

try making listen parameter true:

Provider.of<LoaderProvider>(context, listen: true).showLoader();
amin jamali
  • 360
  • 1
  • 3
  • 16
  • Yep, Already tried but got an error debugIsInInheritedProviderUpdate': Tried to listen to a value exposed with provider, from outside of the widget tree. – Thiru Jul 23 '23 at 13:07
0

If you use Future builder then it will called internally setState so it will change state. So, might be this will possible that state is reset and change UI again. But when you use Provider as State management then you don't need to use Future builder.

AJJ
  • 46
  • 3