简体   繁体   中英

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

this method was working fine if i provide a null value it will not throw error, but after updating to flutter 2.1 now it throws an exception if i provide null value whats the best solution for this please

  /// Method that saves the user lang in shared preferences
  static Future<dynamic> setLanguage (value) async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.setString(_language, value);
  }

[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Invalid argument(s) (value): Must not be null

E/flutter (22502): #0 ArgumentError.checkNotNull (dart:core/errors.dart:185:27) E/flutter (22502): #1
SharedPreferences._setValue (package:shared_preferences/shared_preferences.dart:147:19) E/flutter (22502): #2 SharedPreferences.setString (package:shared_preferences/shared_preferences.dart:133:7)

Check the value then save it:

 static Future<dynamic> setLanguage (value) async {
    if (value != null){
      final SharedPreferences prefs = await SharedPreferences.getInstance();
      return prefs.setString(_language, value);
    }
   return null;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM