Flutter:从StreamBuilder构建器回调内部导航到另一个屏幕

时间:2019-05-13 16:55:29

标签: flutter stream-builder

我有一个初始屏幕和一个StreamBuilder,它发出的状态包含有关身份验证状态的信息。知道身份验证状态后,我想导航到登录页面或主页。但是当我写类似Navigator.of(context).pushReplacement(...)的东西时,我得到了

  

I / flutter(2058):W小工具库引起的异常CA ════════════════════════════   I / flutter(2058):构建StreamBuilder(脏,状态:   我/颤振(2058):_StreamBuilderBaseState>#f4346):   I / flutter(2058):在构建过程中调用了setState()或markNeedsBuild()。   I / flutter(2058):此Overlay小部件不能标记为需要构建,因为该框架已经在   I / flutter(2058年):构建小部件的过程。可以将小部件标记为在构建阶段需要构建   I / flutter(2058):仅当其祖先之一正在建造时。允许此异常是因为框架   I / flutter(2058):在子代之前构建父窗口小部件,这意味着将始终构建脏后代。   I / flutter(2058):否则,框架可能在此构建阶段无法访问此小部件。   I / flutter(2058年):调用了setState()或markNeedsBuild()的小部件是:   I / flutter(2058):Overlay- [LabeledGlobalKey#e0460](状态:OverlayState#ab1a5(条目:   I / flutter(2058):[OverlayEntry#4e962(不透明:false; maintainState:false),OverlayEntry#7656a(不透明:false;   I / flutter(2058):maintainState:true),OverlayEntry#1f86e(不透明:false; maintainState:false),   I / flutter(2058):OverlayEntry#05a15(不透明:false; maintainState:true)]))   I / flutter(2058):发出冒犯电话时当前正在构建的小部件是:   I / flutter(2058):StreamBuilder(脏,状态:_StreamBuilderBaseState>#f4346)   I /颤振(2058):   I / flutter(2058):引发异常时,这是堆栈:   I / flutter(2058):#0 Element.markNeedsBuild。 (包:flutter / src / widgets / framework.dart:3503:11)   I / flutter(2058):#1 Element.markNeedsBuild(package:flutter / src / widgets / framework.dart:3529:6)   I / flutter(2058):#2 State.setState(包:flutter / src / widgets / framework.dart:1133:14)   I / flutter(2058):#3 OverlayState.insertAll(package:flutter / src / widgets / overlay.dart:346:5)   I / flutter(2058):#4 OverlayRoute.install(package:flutter / src / widgets / routes.dart:43:24)   I / flutter(2058):#5 TransitionRoute.install(软件包:flutter / src / widgets / routes.dart:180:11)   I / flutter(2058):#6 ModalRoute.install(软件包:flutter / src / widgets / routes.dart:895:11)   I / flutter(2058):#7 NavigatorState.pushReplacement(包:flutter / src / widgets / navigator.dart:1799:14)   I / flutter(2058):#8 _replace(package:map_chat / application / navigation.dart:75:27)   I / Flutter(2058):#9 _SignInPage.replace(package:map_chat / application / navigation.dart:67:5)   I / flutter(2058):#10 Roadmap.replace(package:map_chat / application / navigation.dart:25:18)   I / Flutter(2058):#11 _SplashPageState._buildPageBasedOnAuthenticationState(package:map_chat / feature / splash.dart:52:19)   I / Flutter(2058):#12 _SplashPageState._buildSplashScreen(package:map_chat / feature / splash.dart:40:11)   I / flutter(2058):#13 _SplashPageState._buildPage。 (package:map_chat / feature / splash.dart:27:18)   I / flutter(2058):#14 StreamBuilder.build(package:flutter / src / widgets / async.dart:425:74)   I / flutter(2058):#15 _StreamBuilderBaseState.build(package:flutter / src / widgets / async.dart:125:48)   I / flutter(2058):#16 StatefulElement.build(package:flutter / src / widgets / framework.dart:3825:27)   I / flutter(2058):#17 ComponentElement.performRebuild(package:flutter / src / widgets / framework.dart:3739:15)   I / flutter(2058):#18 Element.rebuild(包:flutter / src / widgets / framework.dart:3565:5)   I / flutter(2058):#19 BuildOwner.buildScope(package:flutter / src / widgets / framework.dart:2278:33)   I / flutter(2058):#20 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame(package:flutter / src / widgets / binding.dart:700:20)   I / flutter(2058):#21 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback(package:flutter / src / rendering / binding.dart:286:5)   I / flutter(2058):#22 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback(package:flutter / src / scheduler / binding.dart:1012:15)   I / flutter(2058):#23 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame(package:flutter / src / scheduler / binding.dart:952:9)   I / flutter(2058):#24 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.scheduleWarmUpFrame。 (包:flutter / src / scheduler / binding.dart:773:7)   I / Flutter(2058):#33 _Timer._runTimers(dart:isolate-patch / timer_impl.dart:382:19)   I / Flutter(2058):#34 _Timer._handleMessage(dart:isolate-patch / timer_impl.dart:416:5)   I / Flutter(2058):#35 _RawReceivePortImpl._handleMessage(dart:isolate-patch / isolate_patch.dart:171:12)   I / flutter(2058):(从dart:async包和dart:async-patch包中删除了8帧)

我发现的唯一解决方法是使用Future(...).then(navigate)将导航安排到事件队列的末尾,但这很麻烦。这是一个适当的解决方案吗?

2 个答案:

答案 0 :(得分:1)

您可以在构建方法之外侦听流,然后从那里重定向到另一个视图。

答案 1 :(得分:1)

我仍然对解决方案不满意,但是我使用

if(snapshot.hasdata && snapshot.data.navigate) {
     Future.microtask(() => Navigator.of(context).push... );
   }