Reputation: 1583
i am trying to wrap my head around the new riverpod paradigm but fail so far....
before i used a state provider to wrap around my Appstate class, and that worked.... now seemingly i can let riverpod generate the provider itself.... but it won't and i can't get this to work...
so if i understand correctly, the provider takes the form of a function? so the @riverpod applies only to functions i define seemingly, but seems to create a simple provider?
import 'package:riverpod_annotation/riverpod_annotation.dart';
@riverpod
AppState appState(AppStateRef ref) => AppState();
class AppState
{
bool _darkMode = false;
bool _loaded = false;
AppState({bool? darkMode, bool? loaded}) {
if(darkMode!=null) _darkMode = darkMode ;
if(loaded!=null) _loaded = loaded ;
}
bool get darkMode => _darkMode;
set loaded(bool val) => _loaded = val;
set darkMode(bool val) {
_darkMode = val;
}
AppState copyWith({bool? darkMode, bool? loaded}) {
darkMode ?? _darkMode ;
loaded ?? _loaded ;
return AppState(darkMode: darkMode,loaded: loaded);
}
}
(i reduced the example to 1 setting, there will be a lot more than that.... )
but to use it i need a StateProvider, e.g. in the settings page i change the values, those need to be saved in secure storage and a notification issued to force a redraw?
so i am quite lost, i looked at a lot of sites, but my example seems too easy, they allways go for complicated stuff (Todo's stuffed in Lists that get updated asynchronously for example)... didn't found a basic example like this one?
Upvotes: 0
Views: 142
Reputation: 44186
You can use a Notifier that has a ref.listenSelf that will run whenever the state is updated, and use it to marshal the data into your external store. The build() method can fetch the stored data as the initial value.
Upvotes: 0