Flutter/Dart - 延迟后打开视图
Flutter/Dart - Open a view after a delay
我正在尝试找到一种解决方案,以在加载视图出现延迟后打开视图。我有一个视图,其中有一个按钮,当我按下它时,onPressed()
方法会触发 Navigator.of(Context).pushNamed("/loading")
。这将打开加载视图。在加载视图中,我有一个函数可以延迟 1.5 秒,然后打开主视图,这是代码:
Future delay() async{
await new Future.delayed(new Duration(milliseconds: 1500), ()
{
Navigator.of(context).pushNamed("/home");
}
但是,当延迟开始时,在 1.5 秒后,它会打开主页视图,但有以下例外:
I/flutter ( 5226): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 5226): The following assertion was thrown building IconTheme(color: Color(0xff000000)):
I/flutter ( 5226): Multiple widgets used the same GlobalKey.
I/flutter ( 5226): The key [LabeledGlobalKey<ScaffoldState>#a0051] was used by multiple widgets. The parents of those
I/flutter ( 5226): widgets were different widgets that both had the following description:
I/flutter ( 5226): IconTheme(color: Color(0xff000000))
I/flutter ( 5226): A GlobalKey can only be specified on one widget at a time in the widget tree.
I/flutter ( 5226): When the exception was thrown, this was the stack:
I/flutter ( 5226): #0 GlobalKey._debugReserveFor.<anonymous closure> (package:flutter/src/widgets/framework.dart:172:9)
I/flutter ( 5226): #1 GlobalKey._debugReserveFor (package:flutter/src/widgets/framework.dart:182:6)
I/flutter ( 5226): #2 Element.updateChild.<anonymous closure> (package:flutter/src/widgets/framework.dart:2649:13)
I/flutter ( 5226): #3 Element.updateChild (package:flutter/src/widgets/framework.dart:2652:6)
I/flutter ( 5226): #4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #5 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #6 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #7 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #8 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #9 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #10 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #11 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #12 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #13 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #14 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #15 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #16 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #17 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #18 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #19 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #20 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #21 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #22 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #23 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #24 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #25 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #26 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #27 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #28 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #29 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #30 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #31 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #32 ParentDataElement.mount (package:flutter/src/widgets/framework.dart:3893:16)
I/flutter ( 5226): #33 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #34 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4693:32)
I/flutter ( 5226): #35 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #36 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #37 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #38 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #39 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #40 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #41 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #42 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #43 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #44 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #45 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #46 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter ( 5226): #47 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #48 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #49 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #50 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4588:14)
I/flutter ( 5226): #51 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #52 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #53 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #54 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #55 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #56 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #57 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #58 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #59 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4588:14)
I/flutter ( 5226): #60 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #61 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #62 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #63 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #64 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #65 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter ( 5226): #66 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #67 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #68 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #69 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #70 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #71 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #72 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter ( 5226): #73 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #74 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #75 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #76 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #77 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #78 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #79 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #80 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #81 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #82 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #83 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #84 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #85 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #86 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #87 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #88 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #89 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #90 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #91 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter ( 5226): #92 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #93 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #94 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #95 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #96 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #97 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #98 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter ( 5226): #99 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #100 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #101 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #102 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #103 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #104 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #105 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #106 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #107 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #108 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #109 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #110 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #111 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #112 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #113 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #114 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4588:14)
I/flutter ( 5226): #115 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #116 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #117 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4588:14)
I/flutter ( 5226): #118 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #119 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #120 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4588:14)
I/flutter ( 5226): #121 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #122 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #123 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #124 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #125 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #126 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter ( 5226): #127 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #128 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #129 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #130 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #131 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #132 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #133 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #134 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #135 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #136 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4588:14)
I/flutter ( 5226): #137 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #138 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #139 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4588:14)
I/flutter ( 5226): #140 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #141 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #142 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #143 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #144 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #145 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
E/flutter ( 5226): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 5226): NoSuchMethodError: The method 'ancestorStateOfType' was called on null.
E/flutter ( 5226): Receiver: null
E/flutter ( 5226): Tried calling: ancestorStateOfType(Instance of 'TypeMatcher<NavigatorState>')
E/flutter ( 5226): #0 Object.noSuchMethod (dart:core-patch/dart:core/object_patch.dart:46)
E/flutter ( 5226): #1 Navigator.of (package:flutter/src/widgets/navigator.dart:722:19)
E/flutter ( 5226): #2 Navigator.pushReplacementNamed (package:flutter/src/widgets/navigator.dart:666:22)
E/flutter ( 5226): #3 switcher (package:ticketing/router.dart:53:13)
E/flutter ( 5226): #4 _LoadingViewState.switchView (/data/user/0/com.tiziano.flutterapp.flutterapp2/cache/ticketing_remasteredAHSWSC/ticketing_remastered/lib/view/loadingview/LoadingView.dart:79:3)
E/flutter ( 5226): #5 _LoadingViewState.delay.<anonymous closure> (/data/user/0/com.tiziano.flutterapp.flutterapp2/cache/ticketing_remasteredAHSWSC/ticketing_remastered/lib/view/loadingview/LoadingView.dart:74:70)
E/flutter ( 5226): #6 new Future.delayed.<anonymous closure> (dart:async/future.dart:310)
E/flutter ( 5226): #7 Timer._createTimer.<anonymous closure> (dart:async-patch/dart:async/timer_patch.dart:21)
E/flutter ( 5226): #8 _Timer._runTimers (dart:isolate-patch/dart:isolate/timer_impl.dart:367)
E/flutter ( 5226): #9 _Timer._handleMessage (dart:isolate-patch/dart:isolate/timer_impl.dart:401)
E/flutter ( 5226): #10 _RawReceivePortImpl._handleMessage (dart:isolate-patch/dart:isolate/isolate_patch.dart:164)
I/flutter ( 5226): #146 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #147 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #148 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4588:14)
I/flutter ( 5226): #149 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #150 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #151 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #152 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #153 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #154 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter ( 5226): #155 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #156 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #157 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #158 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #159 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #160 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #161 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter ( 5226): #162 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #163 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #164 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #165 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #166 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #167 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #168 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter ( 5226): #169 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #170 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #171 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #172 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:4375:32)
I/flutter ( 5226): #173 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4703:17)
I/flutter ( 5226): #174 Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter ( 5226): #175 _TheatreElement.update (package:flutter/src/widgets/overlay.dart:507:16)
I/flutter ( 5226): #176 Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter ( 5226): #177 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #178 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #179 StatefulElement.update (package:flutter/src/widgets/framework.dart:3737:5)
I/flutter ( 5226): #180 Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter ( 5226): #181 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #182 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #183 ProxyElement.update (package:flutter/src/widgets/framework.dart:3847:5)
I/flutter ( 5226): #184 Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter ( 5226): #185 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4595:14)
I/flutter ( 5226): #186 Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter ( 5226): #187 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #188 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #189 StatefulElement.update (package:flutter/src/widgets/framework.dart:3737:5)
I/flutter ( 5226): #190 Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter ( 5226): #191 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4595:14)
I/flutter ( 5226): #192 Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter ( 5226): #193 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4595:14)
I/flutter ( 5226): #194 Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter ( 5226): #195 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #196 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #197 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2210:33)
I/flutter ( 5226): #198 BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:621:20)
I/flutter ( 5226): #199 BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:208:5)
I/flutter ( 5226): #200 BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:990:15)
I/flutter ( 5226): #201 BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:930:9)
I/flutter ( 5226): #202 BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:842:5)
I/flutter ( 5226): #203 _invoke (file:///b/build/slave/Linux_Engine/build/src/flutter/lib/ui/hooks.dart:120)
I/flutter ( 5226): #204 _drawFrame (file:///b/build/slave/Linux_Engine/build/src/flutter/lib/ui/hooks.dart:109)
I/flutter ( 5226): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter ( 5226): Another exception was thrown: Multiple widgets used the same GlobalKey.
顺便说一下,在主页视图中没有任何黑色图标。 (图标主题(颜色:颜色(0xff000000))
有人可以帮忙吗?
使用 dart:async
库中的 Future.delay
构造函数,以下代码在单击 RaisedButton
3 秒后从第一页导航到第二页,我不明白你为什么要推送正在加载 视图?
import 'dart:async';
class FirstView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new Text("FIRST VIEW"),
new RaisedButton(
onPressed: () async {
await new Future.delayed(const Duration(seconds: 3));
Navigator.push(context, new MaterialPageRoute(builder: (__) => new SecondView()));
},
child: new Text("Go to 2nd view"),
)
],
),
),
);
}
}
class SecondView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new Text("SECOND VIEW"),
),
);
}
}
要在延迟一段时间后打开视图,您可以使用 Future.delayed
:
Future.delayed(const Duration(milliseconds: 500), () {
setState(() {
// Here you can write your code for open new view
});
});
希望对您有所帮助。
您可以通过两种方式完成:
使用 Timer
class.
import 'dart:async';
// Go to Page2 after 5s.
Timer(Duration(seconds: 5), () {
Navigator.push(context, MaterialPageRoute(builder: (_) => Screen2()));
});
使用Future.delayed
class:
// Go to Page2 after 5s.
Future.delayed(Duration(seconds: 5), () {
Navigator.push(context, MaterialPageRoute(builder: (_) => Screen2()));
});
使用 Timer
优于 Future
的好处是它可以取消。
在Android开发中我们使用postDelayed()
回调来保持屏幕。但在 Flutter 中,这非常棘手,因为在 Flutter 中有 async
方法可以执行此类任务。
首先,添加一个从 StatefulWidget 扩展而来的 class。
class SplashScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return SplashState();
}
}
class SplashState extends State<SplashScreen> {
@override
void initState() {
// TODO: implement initState
super.initState();
startTime();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: initScreen(context),
);
}
}
它将覆盖 initState
和 build
方法。在 initState
方法中,我们通过在此处添加这些行来添加延迟功能。
startTime() async {
var duration = new Duration(seconds: 6);
return new Timer(duration, route);
}
startTime
方法是一种在后台执行任务的 async
方法。为此,我们使用 Timer
class 来传递按住屏幕的持续时间,并使用 route/designation 来指示去向。我们使用Navigator
来设置route/designation。这是代码。
对于计时器 class 导入此文件。
import 'dart:async';
这是
的完整代码
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:screens/SecondScreen';
class SplashScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return SplashState();
}
class SplashState extends State<SplashScreen> {
@override
void initState() {
// TODO: implement initState
super.initState();
startTime();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: initScreen(context),
);
}
startTime() async {
var duration = new Duration(seconds: 6);
return new Timer(duration, route);
}
route() {
Navigator.pushReplacement(context, MaterialPageRoute(
builder: (context) => SecondScreen()
)
);
}
initScreen(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
child: Image.asset("images/logo.png"),
),
Padding(padding: EdgeInsets.only(top: 20.0)),
Text(
"Splash Screen",
style: TextStyle(
fontSize: 20.0,
color: Colors.white
),
),
Padding(padding: EdgeInsets.only(top: 20.0)),
CircularProgressIndicator(
backgroundColor: Colors.white,
strokeWidth: 1,
)
],
),
),
);
}
}
更多信息,请参阅:https://medium.com/codechai/lunching-other-screen-after-delay-in-flutter-c9ebf4d7406e
你可以这样使用;
Future myFuture() async {
await new Future.delayed(new Duration(seconds: 3));
//return your_main_future_code_here;
}
ontab: () {
Future.delayed(Duration(seconds: 5), () {
Center(child: CircularProgressIndicator(color: kpurple));
Navigator.of(context).pushNamed('/Home');
});
},
循环进度指示器不工作
如果你只是想延迟代码执行,那么你必须使用 await(没有它就无法工作)
print('Before delay: ${DateTime.now()}');
await Future.delayed(const Duration(milliseconds: 1500));
print('After delay: ${DateTime.now()}');
我正在尝试找到一种解决方案,以在加载视图出现延迟后打开视图。我有一个视图,其中有一个按钮,当我按下它时,onPressed()
方法会触发 Navigator.of(Context).pushNamed("/loading")
。这将打开加载视图。在加载视图中,我有一个函数可以延迟 1.5 秒,然后打开主视图,这是代码:
Future delay() async{
await new Future.delayed(new Duration(milliseconds: 1500), ()
{
Navigator.of(context).pushNamed("/home");
}
但是,当延迟开始时,在 1.5 秒后,它会打开主页视图,但有以下例外:
I/flutter ( 5226): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 5226): The following assertion was thrown building IconTheme(color: Color(0xff000000)):
I/flutter ( 5226): Multiple widgets used the same GlobalKey.
I/flutter ( 5226): The key [LabeledGlobalKey<ScaffoldState>#a0051] was used by multiple widgets. The parents of those
I/flutter ( 5226): widgets were different widgets that both had the following description:
I/flutter ( 5226): IconTheme(color: Color(0xff000000))
I/flutter ( 5226): A GlobalKey can only be specified on one widget at a time in the widget tree.
I/flutter ( 5226): When the exception was thrown, this was the stack:
I/flutter ( 5226): #0 GlobalKey._debugReserveFor.<anonymous closure> (package:flutter/src/widgets/framework.dart:172:9)
I/flutter ( 5226): #1 GlobalKey._debugReserveFor (package:flutter/src/widgets/framework.dart:182:6)
I/flutter ( 5226): #2 Element.updateChild.<anonymous closure> (package:flutter/src/widgets/framework.dart:2649:13)
I/flutter ( 5226): #3 Element.updateChild (package:flutter/src/widgets/framework.dart:2652:6)
I/flutter ( 5226): #4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #5 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #6 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #7 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #8 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #9 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #10 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #11 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #12 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #13 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #14 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #15 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #16 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #17 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #18 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #19 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #20 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #21 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #22 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #23 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #24 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #25 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #26 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #27 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #28 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #29 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #30 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #31 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #32 ParentDataElement.mount (package:flutter/src/widgets/framework.dart:3893:16)
I/flutter ( 5226): #33 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #34 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4693:32)
I/flutter ( 5226): #35 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #36 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #37 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #38 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #39 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #40 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #41 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #42 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #43 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #44 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #45 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #46 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter ( 5226): #47 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #48 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #49 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #50 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4588:14)
I/flutter ( 5226): #51 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #52 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #53 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #54 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #55 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #56 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #57 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #58 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #59 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4588:14)
I/flutter ( 5226): #60 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #61 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #62 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #63 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #64 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #65 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter ( 5226): #66 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #67 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #68 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #69 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #70 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #71 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #72 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter ( 5226): #73 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #74 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #75 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #76 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #77 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #78 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #79 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #80 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #81 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #82 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #83 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #84 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #85 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #86 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #87 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #88 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #89 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #90 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #91 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter ( 5226): #92 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #93 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #94 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #95 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #96 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #97 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #98 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter ( 5226): #99 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #100 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #101 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #102 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #103 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #104 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #105 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #106 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #107 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #108 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #109 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #110 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #111 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #112 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #113 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #114 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4588:14)
I/flutter ( 5226): #115 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #116 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #117 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4588:14)
I/flutter ( 5226): #118 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #119 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #120 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4588:14)
I/flutter ( 5226): #121 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #122 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #123 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #124 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #125 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #126 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter ( 5226): #127 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #128 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #129 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #130 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #131 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #132 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #133 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #134 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #135 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #136 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4588:14)
I/flutter ( 5226): #137 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #138 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #139 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4588:14)
I/flutter ( 5226): #140 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #141 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #142 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #143 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #144 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #145 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
E/flutter ( 5226): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 5226): NoSuchMethodError: The method 'ancestorStateOfType' was called on null.
E/flutter ( 5226): Receiver: null
E/flutter ( 5226): Tried calling: ancestorStateOfType(Instance of 'TypeMatcher<NavigatorState>')
E/flutter ( 5226): #0 Object.noSuchMethod (dart:core-patch/dart:core/object_patch.dart:46)
E/flutter ( 5226): #1 Navigator.of (package:flutter/src/widgets/navigator.dart:722:19)
E/flutter ( 5226): #2 Navigator.pushReplacementNamed (package:flutter/src/widgets/navigator.dart:666:22)
E/flutter ( 5226): #3 switcher (package:ticketing/router.dart:53:13)
E/flutter ( 5226): #4 _LoadingViewState.switchView (/data/user/0/com.tiziano.flutterapp.flutterapp2/cache/ticketing_remasteredAHSWSC/ticketing_remastered/lib/view/loadingview/LoadingView.dart:79:3)
E/flutter ( 5226): #5 _LoadingViewState.delay.<anonymous closure> (/data/user/0/com.tiziano.flutterapp.flutterapp2/cache/ticketing_remasteredAHSWSC/ticketing_remastered/lib/view/loadingview/LoadingView.dart:74:70)
E/flutter ( 5226): #6 new Future.delayed.<anonymous closure> (dart:async/future.dart:310)
E/flutter ( 5226): #7 Timer._createTimer.<anonymous closure> (dart:async-patch/dart:async/timer_patch.dart:21)
E/flutter ( 5226): #8 _Timer._runTimers (dart:isolate-patch/dart:isolate/timer_impl.dart:367)
E/flutter ( 5226): #9 _Timer._handleMessage (dart:isolate-patch/dart:isolate/timer_impl.dart:401)
E/flutter ( 5226): #10 _RawReceivePortImpl._handleMessage (dart:isolate-patch/dart:isolate/isolate_patch.dart:164)
I/flutter ( 5226): #146 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #147 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #148 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4588:14)
I/flutter ( 5226): #149 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #150 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #151 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #152 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #153 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #154 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter ( 5226): #155 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #156 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #157 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #158 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #159 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #160 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #161 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter ( 5226): #162 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #163 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #164 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #165 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #166 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #167 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter ( 5226): #168 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter ( 5226): #169 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter ( 5226): #170 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter ( 5226): #171 Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter ( 5226): #172 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:4375:32)
I/flutter ( 5226): #173 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4703:17)
I/flutter ( 5226): #174 Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter ( 5226): #175 _TheatreElement.update (package:flutter/src/widgets/overlay.dart:507:16)
I/flutter ( 5226): #176 Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter ( 5226): #177 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #178 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #179 StatefulElement.update (package:flutter/src/widgets/framework.dart:3737:5)
I/flutter ( 5226): #180 Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter ( 5226): #181 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #182 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #183 ProxyElement.update (package:flutter/src/widgets/framework.dart:3847:5)
I/flutter ( 5226): #184 Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter ( 5226): #185 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4595:14)
I/flutter ( 5226): #186 Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter ( 5226): #187 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #188 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #189 StatefulElement.update (package:flutter/src/widgets/framework.dart:3737:5)
I/flutter ( 5226): #190 Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter ( 5226): #191 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4595:14)
I/flutter ( 5226): #192 Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter ( 5226): #193 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4595:14)
I/flutter ( 5226): #194 Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter ( 5226): #195 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter ( 5226): #196 Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter ( 5226): #197 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2210:33)
I/flutter ( 5226): #198 BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:621:20)
I/flutter ( 5226): #199 BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:208:5)
I/flutter ( 5226): #200 BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:990:15)
I/flutter ( 5226): #201 BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:930:9)
I/flutter ( 5226): #202 BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:842:5)
I/flutter ( 5226): #203 _invoke (file:///b/build/slave/Linux_Engine/build/src/flutter/lib/ui/hooks.dart:120)
I/flutter ( 5226): #204 _drawFrame (file:///b/build/slave/Linux_Engine/build/src/flutter/lib/ui/hooks.dart:109)
I/flutter ( 5226): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter ( 5226): Another exception was thrown: Multiple widgets used the same GlobalKey.
顺便说一下,在主页视图中没有任何黑色图标。 (图标主题(颜色:颜色(0xff000000)) 有人可以帮忙吗?
使用 dart:async
库中的 Future.delay
构造函数,以下代码在单击 RaisedButton
3 秒后从第一页导航到第二页,我不明白你为什么要推送正在加载 视图?
import 'dart:async';
class FirstView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new Text("FIRST VIEW"),
new RaisedButton(
onPressed: () async {
await new Future.delayed(const Duration(seconds: 3));
Navigator.push(context, new MaterialPageRoute(builder: (__) => new SecondView()));
},
child: new Text("Go to 2nd view"),
)
],
),
),
);
}
}
class SecondView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new Text("SECOND VIEW"),
),
);
}
}
要在延迟一段时间后打开视图,您可以使用 Future.delayed
:
Future.delayed(const Duration(milliseconds: 500), () {
setState(() {
// Here you can write your code for open new view
});
});
希望对您有所帮助。
您可以通过两种方式完成:
使用
Timer
class.import 'dart:async'; // Go to Page2 after 5s. Timer(Duration(seconds: 5), () { Navigator.push(context, MaterialPageRoute(builder: (_) => Screen2())); });
使用
Future.delayed
class:// Go to Page2 after 5s. Future.delayed(Duration(seconds: 5), () { Navigator.push(context, MaterialPageRoute(builder: (_) => Screen2())); });
使用 Timer
优于 Future
的好处是它可以取消。
在Android开发中我们使用postDelayed()
回调来保持屏幕。但在 Flutter 中,这非常棘手,因为在 Flutter 中有 async
方法可以执行此类任务。
首先,添加一个从 StatefulWidget 扩展而来的 class。
class SplashScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return SplashState();
}
}
class SplashState extends State<SplashScreen> {
@override
void initState() {
// TODO: implement initState
super.initState();
startTime();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: initScreen(context),
);
}
}
它将覆盖 initState
和 build
方法。在 initState
方法中,我们通过在此处添加这些行来添加延迟功能。
startTime() async {
var duration = new Duration(seconds: 6);
return new Timer(duration, route);
}
startTime
方法是一种在后台执行任务的 async
方法。为此,我们使用 Timer
class 来传递按住屏幕的持续时间,并使用 route/designation 来指示去向。我们使用Navigator
来设置route/designation。这是代码。
对于计时器 class 导入此文件。
import 'dart:async';
这是
的完整代码import 'dart:async';
import 'package:flutter/material.dart';
import 'package:screens/SecondScreen';
class SplashScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return SplashState();
}
class SplashState extends State<SplashScreen> {
@override
void initState() {
// TODO: implement initState
super.initState();
startTime();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: initScreen(context),
);
}
startTime() async {
var duration = new Duration(seconds: 6);
return new Timer(duration, route);
}
route() {
Navigator.pushReplacement(context, MaterialPageRoute(
builder: (context) => SecondScreen()
)
);
}
initScreen(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
child: Image.asset("images/logo.png"),
),
Padding(padding: EdgeInsets.only(top: 20.0)),
Text(
"Splash Screen",
style: TextStyle(
fontSize: 20.0,
color: Colors.white
),
),
Padding(padding: EdgeInsets.only(top: 20.0)),
CircularProgressIndicator(
backgroundColor: Colors.white,
strokeWidth: 1,
)
],
),
),
);
}
}
更多信息,请参阅:https://medium.com/codechai/lunching-other-screen-after-delay-in-flutter-c9ebf4d7406e
你可以这样使用;
Future myFuture() async {
await new Future.delayed(new Duration(seconds: 3));
//return your_main_future_code_here;
}
ontab: () {
Future.delayed(Duration(seconds: 5), () {
Center(child: CircularProgressIndicator(color: kpurple));
Navigator.of(context).pushNamed('/Home');
});
},
循环进度指示器不工作
如果你只是想延迟代码执行,那么你必须使用 await(没有它就无法工作)
print('Before delay: ${DateTime.now()}');
await Future.delayed(const Duration(milliseconds: 1500));
print('After delay: ${DateTime.now()}');