Home > Enterprise >  unable to watch a bool variable in riverpod
unable to watch a bool variable in riverpod

Time:06-29

i am trying to watch a boolean variable in riverpod so i have the bellow code sample.

final isPro = StateProvider<bool>((_) => false);

class Example extends ConsumerWidget {
  @override
  Widget build(BuildContext context, ScopedReader watch) {
    bool testPro = watch(isPro); //this line returns error
    var totgleBool = context.read(isPro.notifier);

  
    return InkWell(
        onTap: () {
          totgleBool.state = true;
        },
        child: Container(child: testPro ? Text("yes") : Text("no")));
  }
}


so i am getting the following error " The argument type 'StateProvider' can't be assigned to the parameter type 'ProviderBase<Object?, " Thanks.

CodePudding user response:

try this

class Example extends ConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) { // changed here
    bool testPro = ref.watch(isPro); 
    var totgleBool = context.read(isPro.notifier);

  
    return InkWell(
        onTap: () {
          totgleBool.state = true;
        },
        child: Container(child: testPro ? Text("yes") : Text("no")));
  }
}
  • Related