get stored data from sharedpreferences flutter

42 views Asked by At

Please, help me. I dont know what is wrong with this code: trying to get the user passcode stored in preference but geting errot this is my tried code

        'package:shared_preferences/shared_preferences.dart';

        class AuthenticationPinBloc extends Bloc<AuthenticationPinEvent, AuthenticationPinState> {
    final PINRepository pinRepository;
    isUserloggedIn() async {
      // Obtain shared preferences.
      final prefs = await SharedPreferences.getInstance();
      var passcode = prefs.getString('status');
    } 
    AuthenticationPinBloc({required this.pinRepository}) : super(const AuthenticationPinState(pinStatus: AuthenticationPINStatus.enterPIN)) {
      on<AuthenticationPinAddEvent>((event, emit) async {
        String pin = "${state.pin}${event.pinNum}";
        if(pin.length < 6){
          emit(AuthenticationPinState(pin: pin, pinStatus: AuthenticationPINStatus.enterPIN));
        }
        // match pin with store data from SharedPreferences
        else if (pin == passcode){
          print(passcode);
          emit(AuthenticationPinState(pin: pin, pinStatus: AuthenticationPINStatus.equals));

    In the line (pin == passcode) I get an error:

    print passcode also has error am new to Flutter


    will be very glad to get help
1

There are 1 answers

0
jadekim On BEST ANSWER

passcode is possible null, right? i think you can null check.

like followed code.

final prefs = await SharedPreferences.getInstance();
final String passcode = prefs.getString('status');
if (passcode == null)
{
    await prefs.setString('status', '');
    String passcode = '';
} 

i'm not sure this solution. addition, need to your error message.