Skip to content

Instantly share code, notes, and snippets.

@MwBakker
Last active January 30, 2024 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MwBakker/c2c4c573dfe0ec060fb1868ec0fc584a to your computer and use it in GitHub Desktop.
Save MwBakker/c2c4c573dfe0ec060fb1868ec0fc584a to your computer and use it in GitHub Desktop.
Option 1
class MyApp extends ConsumerWidget {
@override
Widget build(BuildContext ctx, WidgetRef ref) =>
ref.watch(settingsAsyncProvider).when(
loading: () => RowLoading(),
error: (e, s) => CustomError(body: '$e \n \n $s'),
data: (data) {
return MaterialApp(
title: 'MyApp',
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
theme: ThemeData(primaryColor: PrimaryColor),
locale: ref.watch(localeProvider),
home: HomePage(settings: data),
);
},
);
}
@riverpod
class SettingsAsyncNotifier extends AsyncNotifier<Settings> {
Future<Settings> _readSettings() async =>
await DataController.fileDB.processSettingsFile();
@override
FutureOr<Settings> build() async => _readSettings();
Future<void> set(Settings settings) =>
update((state) => settings);
Future<void> writeSettings(Settings settings) async {
// settings object is already modified, just need to be written
state = const AsyncValue.loading();
state = await AsyncValue.guard(() async {
await DataController.fileDB.writeSettingsFile(settings);
return settings; // already modified object
});
}
}
final settingsAsyncProvider =
AsyncNotifierProvider<SettingsAsyncNotifier, Settings>(() {
return SettingsAsyncNotifier();
});
InkWell(
child: Icon(Icons.check, size: 32, color: Colors.white70),
onTap: () => ref.read(settingsAsyncProvider).writeSettings(_controller.settings)
),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment