flutter 从共享偏好获取存储数据

bkhjykvo  于 2023-05-01  发布在  Flutter
关注(0)|答案(1)|浏览(104)

请帮帮我我不知道这段代码有什么问题:试图让用户密码存储在首选项,但geting errot这是我尝试的代码

'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
bqf10yzr

bqf10yzr1#

密码可能为null,对吧?我想你可以空头支票。
就像遵循的代码。

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

我不确定这个解决方案。另外,需要您的错误信息。

相关问题