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()));
}
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),
);
}
😉
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With