未处理的异常:无效参数(值):不能为空 Flutter 2

Unhandled Exception: Invalid argument(s) (value): Must not be null Flutter 2

在切换到 Flutter 2 之前,我使用旧版本的 SharedPreferences 没有问题,现在我有了最新的 version.I 能够将 int 类型的值存储在单独的 class 中我从应用程序的各个方面使用,但是在移动和更新所有保存值的尝试之后,我遇到了以下问题:

[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;

      }
    });

  }

SharedPreferencesclass:

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;
  }



}

我不知道如何解决这个问题,也不知道它出现的原因。另一方面,我可以保存字符串值而不会出现这个问题。

你设置了 null 值,所以在新版本的 flutter 中你不能。 确保您的 lang 值不为空。

Future<bool> commit() async {

  _sharedPreferences ??= await SharedPreferences.getInstance();
  if(lang != null){
    await _sharedPreferences.setInt(LANG, lang);
    return true;
  }
  else { // you can set default or return false;
    await _sharedPreferences.setInt(LANG, 1);
    return false;
  }
  return false;

}

等待_sharedPreferences.setInt(LANG, lang);

你的代码中的 lang 为空,新版本 > 2.0.0 中不允许

如果您想将 yaml 中的版本更改为旧版本 或者你可以只给 lang vlaue 来修复错误