How to handle setState in a function ?
I have 2 functions where the user press a button and i save and load the state of that button whenever i need it ,also i store it in sharedPreference.
I want to move that code :
Future<void> _loadPrivacyPolicyAcceptance() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_acceptedPrivacyPolicy = prefs.getBool('acceptedPrivacyPolicy') ?? false;
});
}
Future<void> _savePrivacyPolicyAcceptance(bool accepted) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool('acceptedPrivacyPolicy', accepted);
}
In a new file where i use only functions and not widgets ,but i can’t use setState without a widget.
How i can handle that behaviour ?
2
Answers
Update
Thanks to @Yes Dev for his suggestion to use a
StateSetter
.However, it seems the method needs to be implemented as:
Thus you call it:
Which at some point leads to the issue of not moving the code somewhere else from the widget.
Although there is no "straightforward" way to move the
setState
outside of theStatefulWidget
class, the same concept can be achieved by following a different state management approach.The main goal is to access the state outside the widget class, which can be achieved using the
ChangeNotifier
class.I’d recommend a better understanding of the
ChangeNotifier
andChangeNotifierProvider
– Provider. You could also check: Simple app state management.Example:
1: Create the
ChangeNotifier
class that contains the global states:2- Wrap your main application with a
ChangeNotifierProvider
to make all declared stated inGlobalStates
accessible in the whole app’s widget tree:3- Consume the
ChangeNotifier
(usingProvider.of
):The topic might be a bit broad to be fully covered, however, this should introduce how it can be achieved in its simplest forms. There are several approaches to deal with the state management.
Also, take a look at:
You can pass
setState
to your function and use it how you are now:If you take this approach, you’d need to remove the underscore so the function can be called from another file.
You can reference the official StatefulBuilder widget for inspiration.