Flutter 中的每个数据流或每个 ViewModel 流 - 推荐哪个?

ibn*_*mza 5 dart flutter bloc

我在我的 Flutter 应用程序中使用 BLoC 模式进行状态管理,并且我已经使用这两种方法来管理状态数据。e

1. making each piece of data have its own stream and using StreamBuilder for each widget that needs the data in the UI 

    class LoginBloc{
      bool _isLoggedIn;
      String _username;
      String _password;


      final _isloggedInSubject = BehaviorSubject<bool>();
      StreamSink<bool> get isLoggedInSink => _isloggedInSubject.sink;

      final _usernameSubject = BehaviorSubject<String>();
      StreamSink<String> get userNameSink => _usernameSubject.sink;
      //... //same for the other states

      bool login(){
        _service.Login(_usernameSubject.value,...);
      }
    }

2. Wrapping all page state in a view model and then making just one stream of that viewmodel

    class LoginBloc{
      final loginPageSubject = BehaviorSubject<LoginPageState>();

      bool login(){
        _service.Login(loginPageSubject.value.username, loginPageState.value.password);
      }

    }

    class LoginPageState{
      bool isLoggedIn;
      String username;
      String password;

      LoginPageState({this.isLoggedIn, this.username, this.password});

    }
Run Code Online (Sandbox Code Playgroud)

我相信选项 1 会像这样工作。如果流更改,则只有在其流构建器中使用该流的小部件才会被重建。然而,选项二意味着共享相同状态的所有小部件都会重建,即使它们在状态中绑定的属性没有改变,它们都将重建作为操作 LoginPageState 对象并将其重新添加到通过 _loginPageSubject.sink 流。

与仅需要一个 BehaviorSubject 来管理包含在一个状态对象中的所有状态的选项 2 相比,选项 1 似乎涉及大量代码,如果您有大量的状态数据(大量的 BehaviorSubjects 和 Observables)。使用一个选项比另一个选项有性能提升吗?