Home > database >  PlatformException(Exception encountered, read, java.lang.NullPointerException when using flutter sec
PlatformException(Exception encountered, read, java.lang.NullPointerException when using flutter sec

Time:12-02

When I am get value from flutter(version 2.5.x) secure storage(version 5.0.2) like this:

  static Future<bool> isLoggedIn() async {
    String? username = await SecureStorageUtil.getString("username");
    if (username == null) {
      return false;
    } else {
      return true;
    }
  }

shows error like this:

E/flutter ( 2869): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: PlatformException(Exception encountered, read, java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.content.SharedPreferences.contains(java.lang.String)' on a null object reference
E/flutter ( 2869):  at com.it_nomads.fluttersecurestorage.FlutterSecureStoragePlugin$MethodRunner.run(FlutterSecureStoragePlugin.java:298)
E/flutter ( 2869):  at android.os.Handler.handleCallback(Handler.java:938)
E/flutter ( 2869):  at android.os.Handler.dispatchMessage(Handler.java:99)
E/flutter ( 2869):  at android.os.Looper.loop(Looper.java:223)
E/flutter ( 2869):  at android.os.HandlerThread.run(HandlerThread.java:67)
E/flutter ( 2869): , null)
E/flutter ( 2869): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
E/flutter ( 2869): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:18)
E/flutter ( 2869): <asynchronous suspension>
E/flutter ( 2869): #2      Auth.isLoggedIn (package:wheel/src/biz/auth.dart:27:24)
E/flutter ( 2869): <asynchronous suspension>
E/flutter ( 2869): #3      _SettingsPageState._buildBody.<anonymous closure>.<anonymous closure> (package:reddwarf_dict/pages/settings/settings.dart:48:34)
E/flutter ( 2869): <asynchronous suspension>

what should I do to fix this problem? this is the SecureStorageUtil.getString function:

static FlutterSecureStorage _preferences = FlutterSecureStorage();

  static Future<String?> getString (String key, {String defValue = ''}) {
    return _preferences.read(key:key) ;
  }

CodePudding user response:

I was having the same problem. The fix for me was to clear storage for the app.

Settings -> Apps -> Your App -> Storage -> Clear Storage

Other option is deleteAll() method from flutter storage

  • Related