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

Issue

Before switching to Flutter 2, I was using an old version of SharedPreferences without problems, and now I have the latest version.I was able to store values of type int in a separate class that I use from all aspects of the application, but after moving and updating all attempts to save the values, I have the following problem:

[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):  

I am trying to set values to save the display language that is chosen by the user for the application.

Through the following code:

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:

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



}

I do not know how to solve this problem or the reason for its appearance. On the other hand, I can save String values without this problem.

Solution

Your are set null value, so in new version of flutter you can’t.
Make sure your lang value not to be null.

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;

}

Answered By – Reza M

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.