You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter从SharedPreferences取存储密码报错,请求排查解决

问题排查与解决方案

错误原因分析

  • 变量作用域问题:passcode是isUserloggedIn()方法内的局部变量,外部的事件回调无法访问该变量,导致"未定义标识符"错误。
  • 方法未执行:isUserloggedIn()方法从未被调用,就算作用域没问题,也不会执行获取SharedPreferences的逻辑。
  • 空安全类型不匹配:prefs.getString('status')返回String?类型(可能为null),直接和非空的String类型pin比较会触发类型错误。

解决方案

方案一:提前加载密码到类成员变量

这种方式适合密码不频繁变更的场景,在Bloc初始化时提前加载存储的密码,避免重复IO操作:

import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class AuthenticationPinBloc extends Bloc<AuthenticationPinEvent, AuthenticationPinState> {
  final PINRepository pinRepository;
  String? _storedPasscode; // 类成员变量存储密码

  AuthenticationPinBloc({required this.pinRepository}) : super(const AuthenticationPinState(pinStatus: AuthenticationPINStatus.enterPIN)) {
    // 初始化时提前加载存储的密码
    _loadStoredPasscode();
    on<AuthenticationPinAddEvent>((event, emit) async {
      String pin = "${state.pin}${event.pinNum}";
      if(pin.length < 6){
        emit(AuthenticationPinState(pin: pin, pinStatus: AuthenticationPINStatus.enterPIN));
      } else {
        // 确保密码已加载完成
        if (_storedPasscode == null) {
          await _loadStoredPasscode();
        }
        // 处理空值与匹配逻辑
        if (_storedPasscode != null && pin == _storedPasscode) {
          print(_storedPasscode);
          emit(AuthenticationPinState(pin: pin, pinStatus: AuthenticationPINStatus.equals));
        } else {
          // PIN不匹配的状态处理
          emit(AuthenticationPinState(pin: pin, pinStatus: AuthenticationPINStatus.notEquals));
        }
      }
    });
  }

  // 提取获取密码的方法
  Future<void> _loadStoredPasscode() async {
    final prefs = await SharedPreferences.getInstance();
    _storedPasscode = prefs.getString('status');
  }
}

方案二:事件回调内直接获取密码

这种方式更直接,适合密码可能频繁变更的场景:

import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class AuthenticationPinBloc extends Bloc<AuthenticationPinEvent, AuthenticationPinState> {
  final PINRepository pinRepository;

  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));
      } else {
        // 直接在回调内获取存储的密码
        final prefs = await SharedPreferences.getInstance();
        String? passcode = prefs.getString('status');
        
        if (passcode != null && pin == passcode) {
          print(passcode);
          emit(AuthenticationPinState(pin: pin, pinStatus: AuthenticationPINStatus.equals));
        } else {
          // 处理不匹配或空值的情况
          emit(AuthenticationPinState(pin: pin, pinStatus: AuthenticationPINStatus.notEquals));
        }
      }
    });
  }
}

内容的提问来源于stack exchange,提问作者Obinna Obilo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 16:42:46