Macchiato
Macchiato

Reputation: 541

Unhandled Exception: Null check operator used on a null value shared preference

so I tried Shared preference to make user still logged in until they log out but when i tried to restart the app this appears

[VERBOSE-2:ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value
#0      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:142:86)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:148:36)
#2      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:331:12)
#3      MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:358:49)
#4      MethodChannelSharedPreferencesStore.getAll (package:shared_preferences_platform_interface/method_channel_shared_preferences.dart:44:25)
#5      SharedPreferences._getSharedPreferencesMap (package:shared_preferences/shared_preferences.dart:180:57)
#6      SharedPreferences.getInstance (package:shared_preferences/shared_preferences.dart:56:19)
#7      read_loggedIn (package:indonesia_bisa_2/API/LocalService.dart:11:41)
#8      main (package:indonesia_bisa_2/Main/main.dart:16:14)
#9      _runMainZoned.<anonymous closure>.<…>

heres the checker code

import 'package:shared_preferences/shared_preferences.dart';

Future<void> save_loggedIn() async {
  final prefs = await SharedPreferences.getInstance();
  prefs.setString("loggedIn", "true");
}

//this is for checking the saved variable to check if the user is already logged in or not
Future<String> read_loggedIn() async {
  final prefs = await SharedPreferences.getInstance();
  var value = prefs.getString("loggedIn");
  return value;
}

and here is when the code used

Future<void> main() async {
  runApp(new MaterialApp(
      debugShowCheckedModeBanner: false,
      home: (read_loggedIn() == "true") ? new SPAL (): new MyApp()));
}

Upvotes: 30

Views: 11453

Answers (4)

Defuera
Defuera

Reputation: 5506

Make sure to call WidgetsFlutterBinding.ensureInitialized(); before await SharedPreferences.getInstance();

For example in your main.dart:

void main() async {
  await runZonedGuarded(
    () async {
      WidgetsFlutterBinding.ensureInitialized();

      final prefs = await SharedPreferences.getInstance();
      runApp(MyApp(prefs));
    },
    (error, st) => print(error),
  );
}

😉

Upvotes: 86

Hamed Rezaee
Hamed Rezaee

Reputation: 7222

you can change main method like this:

import 'package:shared_preferences/shared_preferences.dart';

Future<void> save_loggedIn() async {
  final prefs = await SharedPreferences.getInstance();

  prefs.setBool("loggedIn", true);
}

Future<Bool> read_loggedIn() async {
  final prefs = await SharedPreferences.getInstance();
  var value = prefs.getBool("loggedIn");

  return value;
}

Future<void> main() async {
  final bool login = await read_loggedIn();

  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: (login ?? false) ? SPAL() : MyApp(),
    ),
  );
}

Upvotes: 0

Per Gummedal
Per Gummedal

Reputation: 19

I got the exact same error, when I tested my application with iphone simulator. (I have a m1 mac). It may have been a "timing issue". I added this line await Future.delayed(Duration(seconds: 2)); before SharedPreferences.getInstance and the error disappeared.

Upvotes: 1

Saiful Islam
Saiful Islam

Reputation: 1269

The problem is you called read_loggedIn() which is a future but you called it directly and while its not returning value you got a null value. So you have to await until you get the result from read_loggedIn() and then you can use its value.

Replace your main method with bellow code


Future<void> main() async {

    // you have to wait untill get the result
    var login = await read_loggedIn();
    
    runApp(new MaterialApp(
        debugShowCheckedModeBanner: false,
        // and you must check if your login value is null or not
        home: (login != null && login == "true") ? new SPAL(): new MyApp()));
}

Upvotes: -1

Related Questions