Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 从一个小部件更改另一个小部件中的状态_Flutter_Stream_Bloc - Fatal编程技术网

Flutter 从一个小部件更改另一个小部件中的状态

Flutter 从一个小部件更改另一个小部件中的状态,flutter,stream,bloc,Flutter,Stream,Bloc,我正在编写一个flatter应用程序,其中向用户提供一个PageView小部件,允许用户通过滑动在3个“页面”之间导航 我遵循中使用的设置,使用单个类将数据加载到模型中,这应该反映相应的状态更改(IsLoadingData/HasData) 我有一个包含所有ViewPage小部件的主页。页面在MainPageState对象中的构造如下: @override void initState() { _setBloc = SetBloc(widget._repository);

我正在编写一个flatter应用程序,其中向用户提供一个PageView小部件,允许用户通过滑动在3个“页面”之间导航

我遵循中使用的设置,使用单个类将数据加载到模型中,这应该反映相应的状态更改(IsLoadingData/HasData)

我有一个包含所有ViewPage小部件的主页。页面在MainPageState对象中的构造如下:

  @override
  void initState() {
    _setBloc = SetBloc(widget._repository);
    _notificationBloc = NotificationBloc(widget._repository);
    leftWidget = NotificationPage(_notificationBloc);
    middleWidget = SetPage(_setBloc);
    currentPage = middleWidget;
    super.initState();
  } 
如果我们进入NotificationPage,那么它所做的第一件事就是尝试加载数据:

      NotificationPage(this._notificationBloc) {
        _notificationBloc.loadNotificationData();
      }

which should be reflected in the build function when a user directs the application to it:

  //TODO: Consider if state management is correct
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<NotificationState>(
      stream: _notificationBloc.notification.asBroadcastStream(),
      //initialData might be problematic
      initialData: NotificationLoadingState(),
      builder: (context, snapshot) {
        if (snapshot.data is NotificationLoadingState) {
          return _buildLoading();
        }
        if (snapshot.data is NotificationDataState) {
          NotificationDataState state = snapshot.data;
          return buildBody(context, state.notification);
        } else {
          return Container();
        }
      },
    );
  }
通知打印在非通知页的另一页上

我做错了什么。。。。
//....

class _SomeState extends State<SomeWidget> {

 //....
 Stream<int> notificationStream;
 //....

 @override
  void initState() {
   //....
    notificationStream = _notificationBloc.notification.asBroadcastStream()
    super.initState();
  } 

    //....
   Widget build(BuildContext context) {
    return StreamBuilder<NotificationState>(
      stream: notificationStream,
     //....    
类_SomeState扩展了状态{ //.... 流通知流; //.... @凌驾 void initState(){ //.... notificationStream=\u notificationBloc.notification.asBroadcastStream() super.initState(); } //.... 小部件构建(构建上下文){ 返回流生成器( 流:notificationStream, //.... 将流保存到某个位置,并停止每次初始化。 我怀疑build方法被多次调用,因此您创建了一个新流(initState被调用一次)


请尝试让我知道这是否有帮助。

我正在调查这些问题,因为我认为您在创建多个流这一事实上是正确的,但我似乎无法让它正常工作。例如,如果我在主页中检查通知页面的状态,则主页将回答有数据。通知页面将然而,只要继续加载,就好像它没有数据一样。
//....

class _SomeState extends State<SomeWidget> {

 //....
 Stream<int> notificationStream;
 //....

 @override
  void initState() {
   //....
    notificationStream = _notificationBloc.notification.asBroadcastStream()
    super.initState();
  } 

    //....
   Widget build(BuildContext context) {
    return StreamBuilder<NotificationState>(
      stream: notificationStream,
     //....