Sebelum beralih ke Flutter 2, saya menggunakan SharedPreferences versi lama tanpa masalah, dan sekarang saya memiliki versi terbaru. Saya dapat menyimpan nilai tipe int di kelas terpisah yang saya gunakan dari semua aspek aplikasi, tetapi setelah memindahkan dan memperbarui semua upaya untuk menyimpan nilai, saya memiliki masalah berikut:

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

Saya mencoba menetapkan nilai untuk menyimpan bahasa tampilan yang dipilih oleh pengguna untuk aplikasi.

Melalui kode berikut:

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;

      }
    });

  }

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



}

Saya tidak tahu bagaimana mengatasi masalah ini atau alasan kemunculannya. Di sisi lain, saya dapat menyimpan nilai String tanpa masalah ini.

0
M Al 27 Mei 2021, 13:49

1 menjawab

Jawaban Terbaik

Nilai null Anda ditetapkan, jadi dalam versi baru flutter Anda tidak bisa. Pastikan nilai lang Anda tidak nol.

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;

}
1
Reza M 27 Mei 2021, 12:03