update StateProvider with flutter_riverpod

323 views Asked by At

Is it possible to update StateProvider from the Class outside the Widget?

Or I need to use another way?

My code

class SettingScreen extends ConsumerWidget with WidgetsBindingObserver {

This is the place I would like to update the state

     Widget build(BuildContext context, WidgetRef ref) {
     Update and read state working fine 
    }
}
var locationPermissionProvider = StateProvider<bool>((_) => true,);
1

There are 1 answers

0
Kobi On

I found a simple solution and it's working

  • removed WidgetRef & StateProvider

  • changed to StatefulWidget

  • update the state inside setState

  • and change the state in the following function inside the class

    didChangeAppLifecycleState