14

我遵循 BLoC 模式并订阅流,并对构建方法中的状态变化做出反应。加载数据后,我想关闭屏幕。

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Bloc'),
      ),
      body: SafeArea(
        child: StreamBuilder<UserState>(
          stream: _userBloc.user,
          initialData: UserInitState(),
          builder: (context, snapshot) {
            if (snapshot.data is UserInitState) {
              return _buildInit();
            }
            if (snapshot.data is UserDataState) {
              Navigator.pop(context, true);
              return Container();
            }
            if (snapshot.data is UserLoadingState) {
              return _buildLoading();
            }
          },
        ),
      ),
    );
  } 

当我Navigator.pop(context, true);build()方法中做时,我得到:

I/flutter ( 4360): ══╡ EXCEPTION CAUGHT BY ANIMATION LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 4360): The following assertion was thrown while notifying status listeners for AnimationController:
I/flutter ( 4360): setState() or markNeedsBuild() called during build.
I/flutter ( 4360): This Overlay widget cannot be marked as needing to build because the framework is already in the
I/flutter ( 4360): process of building widgets. A widget can be marked as needing to be built during the build phase
I/flutter ( 4360): only if one of its ancestors is currently building. This exception is allowed because the framework
I/flutter ( 4360): builds parent widgets before children, which means a dirty descendant will always be built.
I/flutter ( 4360): Otherwise, the framework might not visit this widget during this build phase.

在 BLoC 模式中处理此类情况的正确方法是什么?

我想出的解决方案之一是开始收听流媒体initState()。在这种情况下,我需要broadcast()我的流,因为我有 2 个订阅者。

有没有更好的解决方案?

4

3 回答 3

16

我想我已经为你找到了解决方案。(请检查一下)

使您的代码看起来像:

Widget build(BuildContext context) {
    // other stuff

    if (snapshot.data is UserDataState) {
      myCallback(() {
        Navigator.pop(context, true);
      });
    }

    // other stuff
}
// after build method (but still in the same class,...) write below method

void myCallback(Function callback) {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      callback();
    });
}

希望能帮助到你。试试吧,请在这里报告以帮助其他人!

来源(flutter_bloc 登录中篇)

描述

于 2019-05-20T11:47:52.667 回答
1
bool hasPop = false;

  @override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: const Text('Bloc'),
    ),
    body: SafeArea(
      child: StreamBuilder<UserState>(
        stream: _userBloc.user,
        initialData: UserInitState(),
        builder: (context, snapshot) {
          if (snapshot.data is UserInitState) {
            return _buildInit();
          }
          if (snapshot.data is UserDataState) {
            if(!hasPop){
               hasPop = true;
               Navigator.pop(context, true);
             }
            
            return Container();
          }
          if (snapshot.data is UserLoadingState) {
            return _buildLoading();
          }
        },
      ),
    ),
  );
} ```
于 2021-02-23T18:29:40.413 回答
0

我可以想象三种可能的解决方案:

1)在我看来,最好重组你的小部件。据我所知,您想要一个“加载屏幕”.. 我认为这没有理由必须是它自己的导航项,而不仅仅是另一个小部件。

IE。你能推那个StreamBuilder吗?升级..因此您的构建器方法如下所示:

if (!snapshot.hasData) {
  return LoadingScreen(snapshot);
}
// Whatever you do with your data.

2)我想我个人会创建一个StatefulWidget,手动收听流initState()并手动调用setState()。不需要一个StreamBuilder

3)作为一种疯狂的解决方法,您可能可以Future(() { Navigator.of(context).pop(); });在您的构建器中使用。(您可能必须使用contextbuild方法,而不是构建器。但无论如何我都不推荐该解决方案)

于 2019-04-13T14:09:35.883 回答