Flutter 2 に切り替える前は、古いバージョンの SharedPreferences を問題なく使用していましたが、現在は最新バージョンを使用しています。 int 型の値を、アプリケーションのあらゆる側面から使用する別のクラスに格納することができましたが、その後値を保存しようとするすべての試みを移動および更新すると、次の問題があります。
[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Invalid argument(s) (value): Must not be null
E/flutter (24208): #0 ArgumentError.checkNotNull (dart:core/errors.dart:185:27)
E/flutter (24208): #1 SharedPreferences._setValue (package:shared_preferences/shared_preferences.dart:147:19)
E/flutter (24208): #2 SharedPreferences.setString (package:shared_preferences/shared_preferences.dart:133:7)
E/flutter (24208): #3 MyPreferences.commit (package:Hasa/MyPreferences.dart:114:30)
E/flutter (24208): #4 _SettingsPageState._handleRadioValueChange.<anonymous closure> (package:Hasa/Settings.dart:149:26)
E/flutter (24208): #5 State.setState (package:flutter/src/widgets/framework.dart:1267:30)
E/flutter (24208): #6 _SettingsPageState._handleRadioValueChange (package:Hasa/Settings.dart:132:5)
E/flutter (24208): #7 _RadioState._handleChanged (package:flutter/src/material/radio.dart:404:24)
E/flutter (24208): #8 RenderToggleable._handleTap (package:flutter/src/material/toggleable.dart:440:19)
E/flutter (24208): #9 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (24208): #10 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:607:11)
E/flutter (24208): #11 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:296:5)
E/flutter (24208): #12 BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:267:7)
E/flutter (24208): #13 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:157:27)
E/flutter (24208): #14 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:385:20)
E/flutter (24208): #15 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:361:22)
E/flutter (24208): #16 RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:278:11)
E/flutter (24208): #17 GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:316:7)
E/flutter (24208): #18 GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:280:5)
E/flutter (24208): #19 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:238:7)
E/flutter (24208): #20 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:221:7)
E/flutter (24208): #21 _rootRunUnary (dart:async/zone.dart:1370:13)
E/flutter (24208): #22 _CustomZone.runUnary (dart:async/zone.dart:1265:19)
E/flutter (24208): #23 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1170:7)
E/flutter (24208): #24 _invoke1 (dart:ui/hooks.dart:180:10)
E/flutter (24208): #25 PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:276:7)
E/flutter (24208): #26 _dispatchPointerDataPacket (dart:ui/hooks.dart:96:31)
E/flutter (24208):
ユーザーがアプリケーション用に選択した表示言語を保存するための値を設定しようとしています。
次のコードを使用します。
void _handleRadioValueChange(int value) {
_radioValue = value;
setState(() {
switch (_radioValue) {
case 1:
SaveLang=1;
context.locale = Locale('en', 'US');
_myPreferences.lang = SaveLang;
_myPreferences.commit();
break;
case 2:
SaveLang=2;
context.locale = Locale('ar', 'SA');
_myPreferences.lang = SaveLang;
_myPreferences.commit();
break;
}
});
}
SharedPreferences クラス:
class MyPreferences {
static const LANG = "lang";
static final MyPreferences instance = MyPreferences._internal();
static SharedPreferences _sharedPreferences;
int lang;
MyPreferences._internal() {}
factory MyPreferences() => instance;
Future<SharedPreferences> get preferences async {
if (_sharedPreferences != null) {
return _sharedPreferences;
} else {
_sharedPreferences = await SharedPreferences.getInstance();
lang = _sharedPreferences.getInt(LANG);
return _sharedPreferences;
}
}
Future<bool> commit() async {
_sharedPreferences ??= await SharedPreferences.getInstance();
await _sharedPreferences.setInt(LANG, lang);
}
Future<MyPreferences> init() async {
_sharedPreferences = await preferences;
return this;
}
}
この問題を解決する方法や、その出現理由がわかりません。一方、この問題なしで String 値を保存できます。