如何清除bloc中的数据?

时间:2019-03-13 09:21:38

标签: dart flutter bloc

就目前而言,当用户尝试单击登录时,该按钮将变为进度栏。我遇到的麻烦是我无法使用流将进度条变成一个按钮。

我正在复制我在网上找到的教程,我正试图对其进行修改以适合我的需求,所以我还不是100%地了解集团。

这是我正在关注的教程

https://medium.com/flutterpub/when-firebase-meets-bloc-pattern-fb5c405597e0

这是登录按钮

  Widget loginButton() {
    return StreamBuilder(
        stream: _bloc.signInStatus,
        builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
          if (!snapshot.hasData || snapshot.hasError) {
            return button();
          } else {
            return LinearProgressIndicator();
          }
        });
  }

  Widget button() {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: 16.0),
      child: MaterialButton(
          minWidth: 200.0,
          height: 42.0,
          child: Text(
            StringConstant.submit,
            style: TextStyle(color: Colors.white),
          ),
          color: ThemeSettings.RaisedButton,
          onPressed: () {
            if (_bloc.validateFields()) {
              authenticateUser();
            } else {
              showAlertDialog(context, "Verification Failed",
                  "The Email / Number you entered couldn't be found or your password was incorrect. Please try again.");
          }),
    );
  }

  void authenticateUser() {
    _bloc.showProgressBar(true);
    _bloc.authenticateUser().then((value) {
      //Username and password ARE correct
      if (value) {
        Navigator.pushReplacement(
            context,
            MaterialPageRoute(
                builder: (context) => EventsList(_bloc.emailAddress)));
      } else {
        print("this one");
        showAlertDialog(context, "Verification Failed",
            "The Email / Number you entered couldn't be found or your password was incorrect. Please try again.");
        // I believe I need to clear what is inside of snapshot here?
      }
    });
  }

在我的login_bloc.dart内部,我认为重要的是要知道什么?

class LoginBloc {
  final _repository = Repository();
  final _email = BehaviorSubject<String>();
  final _password = BehaviorSubject<String>();
  final _firstName = BehaviorSubject<String>();
  final _phoneNumber = BehaviorSubject<int>();
  final _profilePicture = BehaviorSubject<String>();
  final _isSignedIn = BehaviorSubject<bool>();

  Observable<String> get email => _email.stream.transform(_validateEmail);

  Observable<String> get password =>
      _password.stream.transform(_validatePassword);

  Observable<bool> get signInStatus => _isSignedIn.stream;

  String get emailAddress => _email.value;

  // Change data
  Function(String) get changeEmail => _email.sink.add;

  Function(String) get changePassword => _password.sink.add;

  Function(bool) get showProgressBar => _isSignedIn.sink.add;

  final _validateEmail =
      StreamTransformer<String, String>.fromHandlers(handleData: (email, sink) {
    if (email.contains('@')) {
      sink.add(email);
    } else {
      sink.addError(StringConstant.emailValidateMessage);
    }
  });

  final _validatePassword = StreamTransformer<String, String>.fromHandlers(
      handleData: (password, sink) {
    if (password.length >= 6) {
      sink.add(password);
    } else {
      sink.addError(StringConstant.passwordValidateMessage);
    }
  });

  Future<bool> authenticateUser() {
    return _repository.authenticateUser(_email.value, _password.value);
  }

  Future<AuthenticatedStatus> doesUserExist() {
    return _repository.doesUserExist(_email.value);
  }

  Future<void> addUserToDatabase() {
    return _repository.addUserToDatabase(_email.value, _password.value,
        _firstName.value, _phoneNumber.value, _profilePicture.value);
  }

  bool validateFields() {
    if (_email.value != null &&
        _email.value.isNotEmpty &&
        _password.value != null &&
        _password.value.isNotEmpty &&
        _email.value.contains('@') &&
        _password.value.length >= 6) {
      return true;
    } else {
      return false;
    }
  }

  void dispose() async {
    await _email.drain();
    _email.close();
    await _password.drain();
    _password.close();
    await _firstName.drain();
    _firstName.close();
    await _phoneNumber.drain();
    _phoneNumber.close();
    await _profilePicture.drain();
    _profilePicture.close();
    await _isSignedIn.drain();
    _isSignedIn.close();
  }

1 个答案:

答案 0 :(得分:0)

我还没有尝试过博客文章中提供的示例,但是通过这些片段,看起来一个简单的 _bloc.showProgressBar(false); 应该可以解决问题。你试过这样做吗?