skip to Main Content

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?

2

Answers


  1. 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

    Login or Signup to reply.
  2. try making listen parameter true:

    Provider.of<LoaderProvider>(context, listen: true).showLoader();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search