diff --git a/.vscode/lemmur.code-snippets b/.vscode/lemmur.code-snippets index 815ff39..8f926b5 100644 --- a/.vscode/lemmur.code-snippets +++ b/.vscode/lemmur.code-snippets @@ -28,6 +28,6 @@ }, "L10n string": { "prefix": "l10n", - "body": ["L10n.of(context).$0"] + "body": ["L10n.of(context)!.$0"] } } diff --git a/lib/l10n/l10n.dart b/lib/l10n/l10n.dart index 3484ed9..ccf6c2a 100644 --- a/lib/l10n/l10n.dart +++ b/lib/l10n/l10n.dart @@ -6,8 +6,8 @@ export 'l10n_api.dart'; export 'l10n_from_string.dart'; abstract class LocaleSerde { - static Locale? fromJson(String? json) { - if (json == null) return null; + static Locale fromJson(String? json) { + if (json == null) return const Locale('en'); final lang = json.split('-'); diff --git a/lib/stores/accounts_store.g.dart b/lib/stores/accounts_store.g.dart index 2b909ae..c041829 100644 --- a/lib/stores/accounts_store.g.dart +++ b/lib/stores/accounts_store.g.dart @@ -8,20 +8,19 @@ part of 'accounts_store.dart'; AccountsStore _$AccountsStoreFromJson(Map json) { return AccountsStore() - ..tokens = (json['tokens'] as Map)?.map( + ..tokens = (json['tokens'] as Map?)?.map( (k, e) => MapEntry( k, - (e as Map)?.map( - (k, e) => - MapEntry(k, e == null ? null : Jwt.fromJson(e as String)), + (e as Map).map( + (k, e) => MapEntry(k, Jwt.fromJson(e as String)), )), ) ?? {'lemmy.ml': {}} - ..defaultAccounts = (json['defaultAccounts'] as Map)?.map( + ..defaultAccounts = (json['defaultAccounts'] as Map?)?.map( (k, e) => MapEntry(k, e as String), ) ?? {} - ..defaultAccount = json['defaultAccount'] as String; + ..defaultAccount = json['defaultAccount'] as String?; } Map _$AccountsStoreToJson(AccountsStore instance) => diff --git a/lib/stores/config_store.dart b/lib/stores/config_store.dart index f27822a..33b4a0f 100644 --- a/lib/stores/config_store.dart +++ b/lib/stores/config_store.dart @@ -33,11 +33,11 @@ class ConfigStore extends ChangeNotifier { save(); } - Locale? _locale; - // default value is set in the `load` method because json_serializable does - // not accept non-literals as constant values + late Locale _locale; + // default value is set in the `LocaleSerde.fromJson` method because json_serializable does + // not accept non-literals as defaultValue @JsonKey(fromJson: LocaleSerde.fromJson, toJson: LocaleSerde.toJson) - Locale get locale => _locale ?? const Locale('en'); + Locale get locale => _locale; set locale(Locale locale) { _locale = locale; notifyListeners(); diff --git a/lib/stores/config_store.g.dart b/lib/stores/config_store.g.dart index 2268fc6..af41b4a 100644 --- a/lib/stores/config_store.g.dart +++ b/lib/stores/config_store.g.dart @@ -10,8 +10,8 @@ ConfigStore _$ConfigStoreFromJson(Map json) { return ConfigStore() ..theme = _$enumDecodeNullable(_$ThemeModeEnumMap, json['theme']) ?? ThemeMode.system - ..amoledDarkMode = json['amoledDarkMode'] as bool ?? false - ..locale = LocaleSerde.fromJson(json['locale'] as String); + ..amoledDarkMode = json['amoledDarkMode'] as bool? ?? false + ..locale = LocaleSerde.fromJson(json['locale'] as String?); } Map _$ConfigStoreToJson(ConfigStore instance) => @@ -21,36 +21,41 @@ Map _$ConfigStoreToJson(ConfigStore instance) => 'locale': LocaleSerde.toJson(instance.locale), }; -T _$enumDecode( - Map enumValues, - dynamic source, { - T unknownValue, +K _$enumDecode( + Map enumValues, + Object? source, { + K? unknownValue, }) { if (source == null) { - throw ArgumentError('A value must be provided. Supported values: ' - '${enumValues.values.join(', ')}'); + throw ArgumentError( + 'A value must be provided. Supported values: ' + '${enumValues.values.join(', ')}', + ); } - final value = enumValues.entries - .singleWhere((e) => e.value == source, orElse: () => null) - ?.key; - - if (value == null && unknownValue == null) { - throw ArgumentError('`$source` is not one of the supported values: ' - '${enumValues.values.join(', ')}'); - } - return value ?? unknownValue; + return enumValues.entries.singleWhere( + (e) => e.value == source, + orElse: () { + if (unknownValue == null) { + throw ArgumentError( + '`$source` is not one of the supported values: ' + '${enumValues.values.join(', ')}', + ); + } + return MapEntry(unknownValue, enumValues.values.first); + }, + ).key; } -T _$enumDecodeNullable( - Map enumValues, +K? _$enumDecodeNullable( + Map enumValues, dynamic source, { - T unknownValue, + K? unknownValue, }) { if (source == null) { return null; } - return _$enumDecode(enumValues, source, unknownValue: unknownValue); + return _$enumDecode(enumValues, source, unknownValue: unknownValue); } const _$ThemeModeEnumMap = {