Last active
January 30, 2024 16:51
-
-
Save MwBakker/c2c4c573dfe0ec060fb1868ec0fc584a to your computer and use it in GitHub Desktop.
Option 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | |
); | |
}, | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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