简体   繁体   English

如何使用Bloc在颤抖中导航

[英]How to navigate in flutter with bloc

I have a login/signup page. 我有一个登录/注册页面。 When a user sign up, I call my bloc code and do firebase signup. 当用户注册时,我会调用我的集团代码并进行Firebase注册。 Now when I complete the signup I want the app to be redirected to the home page. 现在,当我完成注册后,我希望将应用程序重定向到主页。 How can I can I control the navigation flow from the bloc? 如何控制集团的导航流程?

I tried using firebase authstatechanges stream in the 'home' of material app but that only works when the app first starts. 我尝试在实体应用的“主目录”中使用firebase authstatechanges流,但这仅在应用首次启动时有效。 It does not work after I have already loaded the signup page. 我已经加载了注册页面后,它不起作用。

Also I created a method just to listen to the stream boolean value that gets changed when the user is signed up but that method is not receiving any updates. 我还创建了一个方法,仅用于侦听在用户注册后更改的流布尔值,但是该方法未收到任何更新。

class EventsBloc{

    final FirebaseAuth firebaseAuth = FirebaseAuth.instance;

    Sink<User> get doSignup => _signupController.sink;

    final _signupController = StreamController<User>();

    Stream<bool> get isLoading => _isLoadingSubject.stream;

    final _isLoadingSubject = BehaviorSubject<bool>(seedValue: false);



    EventsBloc()
    {
      _signupController.stream.listen((user){

        _isLoadingSubject.add(true);
        firebaseAuth.createUserWithEmailAndPassword(email: user.email, password: user.password).then((firebaseUser) {

          _isLoadingSubject.add(false);
           // I WANT TO NAVIGATE TO THE HOME PAGE HERE
          Firestore.instance.collection('user').document()
              .setData({ 'email': user.email, 'phone': user.phoneNumber, 'name': user.name });


        });

      });
    }


  }

I think the auth state stream in a parent StreamBuilder should work? 我认为父StreamBuilder的身份验证状态流应该工作吗? That would be the nicest way in my opinion. 我认为那将是最好的方法。

An option is to add a new stream to your bloc, like Stream<String> get doNavigate , and add events to it when you want the widget to navigate somewhere. 一种选择是将新的流添加到您的块中,例如Stream<String> get doNavigate ,并在您希望小部件导航到某处时向其中添加事件。 You can think of navigation actions as just another output stream from your bloc. 您可以将导航操作视为来自集团的另一个输出流。

Yet another option is to expand the scope of isLoading to include information like login status too. 另一个选择是扩展isLoading的范围, isLoading也包括登录状态之类的信息。 You could have an enum with all possible statuses like: 您可以拥有一个具有所有可能状态的枚举,例如:

enum Status { signedOut, loading, signedIn }

And then expose it from your bloc instead of just the loading state: 然后从您的bloc中公开它,而不仅仅是加载状态:

class EventBloc {
  // ...
  Stream<Status> get status => _statusSubject.stream;
  final _statusSubject = BehaviorSubject<Status>(seedValue: Status.signedOut);

  EventsBloc() {
    _signupController.stream.listen((user) async {
      _statusSubject.add(Status.loading);
      final firebaseUser = await firebaseAuth.createUserWithEmailAndPassword(
        email: user.email,
        password: user.password,
      );
      Firestore.instance.collection('user').document().setData(
          {'email': user.email, 'phone': user.phoneNumber, 'name': user.name});
      _statusSubject.add(Status.signedIn);
    });
  }
}```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM