Flutter 如何在Bloc和rxdart模式中使用SharedReference?

Flutter 如何在Bloc和rxdart模式中使用SharedReference?,flutter,dart,Flutter,Dart,如何保存API响应?我已经像下面的编码一样完成了。起初我成功了,但现在不行了。我的编码有问题吗。也许有人能帮我 登录区: 在这里,我获取API响应并将它们保存在SharedReferences中 class LoginBloc extends Object with Validators{ final _repository = EresidenceRepository(); final _userid = BehaviorSubject<String>(); final

如何保存API响应?我已经像下面的编码一样完成了。起初我成功了,但现在不行了。我的编码有问题吗。也许有人能帮我

登录区: 在这里,我获取API响应并将它们保存在SharedReferences中

class LoginBloc extends Object with Validators{
  final _repository = EresidenceRepository();
  final _userid = BehaviorSubject<String>();
  final _password = BehaviorSubject<String>();
  final _imei = BehaviorSubject<String>();
  final _coordinate = BehaviorSubject<String>();
  final BehaviorSubject<ApiResponse<login_responses>> _subject = BehaviorSubject<ApiResponse<login_responses>>();

  Function(String) get userid => _userid.sink.add;
  Function(String) get password => _password.sink.add;
  Function(String) get imei => _imei.sink.add;
  Function(String) get coordinate => _coordinate.sink.add;

  Stream<String> get useridValidation => _userid.stream.transform(useridValidator);
  Stream<String> get passwordValidation => _password.stream.transform(passwordValidator);
  Stream<bool> get submitCheck => Rx.combineLatest2(useridValidation, passwordValidation, (e,p) => true);

  login() async {
    _subject.sink.add(ApiResponse.loading("Logging In..."));
    try {
      login_responses response = await _repository.login(
          _userid.value, _password.value, _imei.value, _coordinate.value);

//      Save responses
      prefsBloc.changePrefsLogin(
          PrefsState(false, response.data.userid, response.data.password, _imei.value, _coordinate.value, "")
      );

      _subject.sink.add(ApiResponse.completed(response));

      print(response);

    } catch (e) {
      _subject.sink.add(ApiResponse.error(e.toString()));
      print(e);
    }
  }

  dispose(){
    _userid.close();
    _password.close();
    _imei.close();
    _coordinate.close();
    _subject.close();
  }

  BehaviorSubject<ApiResponse<login_responses>> get subject => _subject;

}
类LoginBloc使用验证器扩展对象{
最终_repository=EresidenceRepository();
final _userid=BehaviorSubject();
最终密码=行为主体();
final _imei=行为主体();
最终坐标=行为主体();
最终行为主体_subject=行为主体();
函数(字符串)get userid=>\u userid.sink.add;
函数(字符串)get password=>\u password.sink.add;
函数(字符串)get-imei=>\u-imei.sink.add;
函数(字符串)get coordinate=>\u coordinate.sink.add;
Stream get useridValidation=>\u userid.Stream.transform(useridValidator);
Stream get passwordValidation=>\u password.Stream.transform(passwordValidator);
Stream get submitCheck=>Rx.combineLatest2(useridValidation,passwordValidation,(e,p)=>true);
login()异步{
_subject.sink.add(ApiResponse.load(“登录…”));
试一试{
login\u responses response=wait\u repository.login(
_userid.value、_password.value、_imei.value、_coordinate.value);
//保存响应
prefsBloc.changePrefsLogin(
PrefsState(false,response.data.userid,response.data.password,_imei.value,_coordinate.value,“”)
);
_subject.sink.add(ApiResponse.completed(response));
打印(回复);
}捕获(e){
_subject.sink.add(ApiResponse.error(例如toString());
印刷品(e);
}
}
处置{
_userid.close();
_password.close();
_imei.close();
_coordinate.close();
_subject.close();
}
行为主体get subject=>\u subject;
}
SharedReferences集团:

此bloc用于存储和检索数据的过程

class PrefsState {
  final bool checklogin;
  final String userid;
  final String password;
  final String imei;
  final String coordinate;
  final String bit61;

  PrefsState(this.checklogin, this.userid, this.password, this.imei, this.coordinate, this.bit61);
}

class PrefsBloc {
  final _changePrefernce = BehaviorSubject<PrefsState>();
  Function(PrefsState) get changePrefsLogin => _changePrefernce.sink.add;
  Stream<PrefsState> get prefrenceLogin => _changePrefernce.stream;
  SharedPreferences sPrefs;

  dispose(){
    _changePrefernce?.close();
  }

  PrefsBloc(){
    _loadLoginPref();
    _changePrefernce.stream.listen(_saveLoginPref);
  }

  Future<void> _loadLoginPref() async {
    sPrefs = await SharedPreferences.getInstance();
    final checkPref = sPrefs.getBool("checkLogin") ?? false;
    final useridPref = sPrefs.getString("userid") ?? "";
    final passwordPref = sPrefs.getString("password") ?? "";
    final imeiPref = sPrefs.getString("imei") ?? "";
    final coordinatePref = sPrefs.getString("coordinate") ?? "";
    final bit61 = sPrefs.getString("bit61") ?? "";
    _changePrefernce.add(PrefsState(checkPref, useridPref, passwordPref, imeiPref, coordinatePref, bit61));
  }

  void _saveLoginPref(PrefsState stats){
    if (stats != null) {
      if (sPrefs != null) {
        sPrefs.setBool("checkLogin", stats.checklogin);
        sPrefs.setString("userid", stats.userid);
        sPrefs.setString("password", stats.password);
        sPrefs.setString("imei", stats.imei);
        sPrefs.setString("coordinate", stats.coordinate);
        sPrefs.setString("bit61", stats.bit61);
        sPrefs.commit();
      }
    }
  }

  Future<void> removeLoginPref(BuildContext context) async{
    sPrefs = await SharedPreferences.getInstance();
    if(sPrefs != null) {
      await sPrefs.clear();
      AppRoutes.pushAndRemoveUntil(context, LoginPage());
    }
  }

  Future<void> checkLoginPref(BuildContext context) async{
    sPrefs = await SharedPreferences.getInstance();
    if(sPrefs.getBool("checkLogin") == true){
      AppRoutes.pushAndRemoveUntil(context, MainApp());
    }
  }

}

final prefsBloc = PrefsBloc();
类预状态{
最终bool检查登录;
最终字符串用户标识;
最终字符串密码;
最终字符串imei;
最终串坐标;
最后的字符串位61;
PrefsState(this.checklogin、this.userid、this.password、this.imei、this.coordinate、this.bit61);
}
类PrefsBloc{
最终_changePreference=行为主体();
函数(PrefsState)get changePrefsLogin=>\u changePreference.sink.add;
Stream get prefrenceLogin=>\u changePreference.Stream;
共享引用SPref;
处置{
_ChangePreference?.close();
}
PrefsBloc(){
_loadLoginPref();
_changePreference.stream.listen(\u saveLoginPref);
}
Future\u loadLoginPref()异步{
sPrefs=等待SharedReferences.getInstance();
final checkPref=sPrefs.getBool(“checkLogin”)??false;
final useridPref=sPrefs.getString(“userid”);
final passwordPref=sPrefs.getString(“密码”);
最终imeiPref=sPrefs.getString(“imei”);
最终坐标ref=sPrefs.getString(“坐标”);
最终位61=sPrefs.getString(“位61”)?“”;
_添加(PrefsState(checkPref、useridPref、passwordPref、imeiPref、coordinatePref、bit61));
}
void\u saveLoginPref(预状态统计){
if(stats!=null){
if(sPrefs!=null){
setBool(“checkLogin”,stats.checkLogin);
设置字符串(“userid”,stats.userid);
设置字符串(“密码”,stats.password);
设置字符串(“imei”,stats.imei);
设置字符串(“坐标”,统计坐标);
设置字符串(“位61”,统计数据位61);
commit();
}
}
}
Future removeLoginPref(构建上下文)异步{
sPrefs=等待SharedReferences.getInstance();
if(sPrefs!=null){
等待sPrefs.clear();
pushandremoveintil(上下文,LoginPage());
}
}
Future CheckLoginRef(构建上下文)异步{
sPrefs=等待SharedReferences.getInstance();
if(sPrefs.getBool(“checkLogin”)==true){
pushandremoveintil(上下文,MainApp());
}
}
}
最终prefsBloc=prefsBloc();