微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Flutter中访问嵌套StreamBuilder中的流

如何解决在Flutter中访问嵌套StreamBuilder中的流

我在另一个位于RasedButton小部件的onpressed字段内的StreamBuilder中调用StreamBuilder时遇到问题。基本上,我试图在内部StreamBuilder中访问流(向其中添加数据之后),但是执行过程不会调用代码的这一部分:

Widget submitButton(BuildContext ctx,AccountBloc bloc){
   return StreamBuilder(
      stream: bloc.submitValid,builder: (context,snapshot){
        return SizedBox(
          width: double.infinity,height: 60,child: RaisedButton(
            color: HexColor("0072b1"),child: Text("Sign in",style: TextStyle(
              fontWeight: FontWeight.bold,fontSize: 20,color: Colors.white
            ),),onpressed:() {
               if(snapshot.hasData){
                 bloc.loginUser();
             
                 // DEBUGGING
                 print(bloc.isAuthenticated.listen((val) { print("debug isAuthenticated: $val"); }));
                  StreamBuilder(
                     stream: bloc.isAuthenticated,builder: (ctx,snapshotA){
                       print("here");
                       if(!snapshotA.hasData){
                         return Text("loading....");
                       }
                       print("***${snapshotA.data}****");
                       if(snapshotA.data == false){
                         return navSignUpScreen(ctx);
                       }else if (snapshotA.data == false){
                         print("here");
                         return navHomeScreen(ctx);
                       }
                       return navSignUpScreen(ctx);
                     }
                 );
               }
            },);
      }
  );
}

BLOC部分如下:

  final _isAuthenticated = BehaviorSubject<bool>();
  Stream<bool> get isAuthenticated => _isAuthenticated.stream;

  void loginUser() async {
    var inMemory = InMemoryProvider();
    inMemory.newInMemoryProvider();

    usermodel userResult = await _repository.loginUser(_email.value,_password.value);
    if(userResult.status == 200){
      // save TOKEN
      usermodel usermodel = usermodel.fromJsonToModel(userResult.data);
      bool res =  await inMemory.store(usermodel.id,usermodel.token);
      _isAuthenticated.sink.add(res);
    }
    _userLoginResponse.sink.add(userResult);
  }

navHomeScreen定义很简单:

class HomeScreen extends StatefulWidget {
  createState() {
    return HomeState();
  }
}


class HomeState extends State<HomeScreen> {

  int _currentIndex = 0;

  final List<Widget> _children = [
    AllReportsScreen(),UserProfileScreen()
  ];

  Widget build(context) {
    return Scaffold(
      body: _children[_currentIndex],bottomNavigationBar: mainBottomNavigatorBar(),);
  }

  Widget mainBottomNavigatorBar() {
    return BottomNavigationBar(
      type: BottomNavigationBarType.fixed,onTap: onTabTapped,currentIndex: _currentIndex,items: [
        BottomNavigationBarItem(
          backgroundColor: Colors.black45,icon: new Icon(Icons.note),title: new Text('Reports',style: TextStyle(fontSize: 15.0)),BottomNavigationBarItem(
          backgroundColor: Colors.black45,icon: new Icon(Icons.label_important),title: new Text('Attention',icon: new Icon(Icons.person_pin),title: new Text('Profile',],);
  }


  void onTabTapped(int index) {
    setState(() {
      _currentIndex = index;
    });
  }

}

解决方法

使用constraint row1 = forall([...]); 中的combineLatest将两个rxdartStream组合成一个isAuthenticated

submitValid

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。