Flutter填坑记录

在Drawer的子页面进行全局数据修改报错

flutter doctor

1
2
3
4
5
6
7
8
[√] Flutter (Channel stable, v1.7.8+hotfix.3, on Microsoft Windows [Version 10.0.17763.503], locale zh-CN)

[√] Android toolchain - develop for Android devices (Android SDK version 29.0.0)
[√] Android Studio (version 3.3)
[!] IntelliJ IDEA Ultimate Edition (version 2018.3)
X Flutter plugin not installed; this adds Flutter specific functionality.
X Dart plugin not installed; this adds Dart specific functionality.
[√] Connected device (1 available)

详细错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
I/flutter (24904): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (24904): The following assertion was thrown while finalizing the widget tree:
I/flutter (24904): setState() or markNeedsBuild() called when widget tree was locked.
I/flutter (24904): This _ModalScope<dynamic> widget cannot be marked as needing to build because the framework is
I/flutter (24904): locked.
I/flutter (24904): The widget on which setState() or markNeedsBuild() was called was:
I/flutter (24904): _ModalScope<dynamic>-[LabeledGlobalKey<_ModalScopeState<dynamic>>#df519]
I/flutter (24904):
I/flutter (24904): When the exception was thrown, this was the stack:
I/flutter (24904): #0 Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:3675:9)
I/flutter (24904): #1 Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:3685:6)
I/flutter (24904): #2 State.setState (package:flutter/src/widgets/framework.dart:1161:14)
I/flutter (24904): #3 _ModalScopeState._routeSetState (package:flutter/src/widgets/routes.dart:633:5)
I/flutter (24904): #4 ModalRoute.setState (package:flutter/src/widgets/routes.dart:732:30)
I/flutter (24904): #5 ModalRoute.changedInternalState (package:flutter/src/widgets/routes.dart:1196:5)
I/flutter (24904): #6 _ModalRoute&TransitionRoute&LocalHistoryRoute.removeLocalHistoryEntry (package:flutter/src/widgets/routes.dart:502:7)
I/flutter (24904): #7 LocalHistoryEntry.remove (package:flutter/src/widgets/routes.dart:333:12)
I/flutter (24904): #8 DrawerControllerState.dispose (package:flutter/src/material/drawer.dart:250:20)
I/flutter (24904): #9 StatefulElement.unmount (package:flutter/src/widgets/framework.dart:4107:12)
I/flutter (24904): #10 _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1737:13)
I/flutter (24904): #11 _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1735:7)
I/flutter (24904): #12 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3955:14)
I/flutter (24904): #13 _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1733:13)
I/flutter (24904): #14 _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1735:7)
I/flutter (24904): #15 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3955:14)
I/flutter (24904): #16 _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1733:13)
I/flutter (24904): #17 _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1735:7)
I/flutter (24904): #18 MultiChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:5181:16)
I/flutter (24904): #19 _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1733:13)
I/flutter (24904): #20 _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1735:7)
I/flutter (24904): #21 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3955:14)
I/flutter (24904): #22 _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1733:13)
I/flutter (24904): #23 _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1735:7)
I/flutter (24904): #24 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3955:14)
I/flutter (24904): #25 _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1733:13)
I/flutter (24904): #26 _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1735:7)
I/flutter (24904): #27 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3955:14)
I/flutter (24904): #28 _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1733:13)
I/flutter (24904): #29 _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1735:7)
I/flutter (24904): #30 SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:5080:14)
I/flutter (24904): #31 _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1733:13)
I/flutter (24904): #32 _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1735:7)
I/flutter (24904): #33 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3955:14)
I/flutter (24904): #34 _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1733:13)
I/flutter (24904): #35 _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1735:7)
I/flutter (24904): #36 SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:5080:14)
I/flutter (24904): #37 _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1733:13)
I/flutter (24904): #38 _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1735:7)
I/flutter (24904): #39 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3955:14)
I/flutter (24904): #40 _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1733:13)
I/flutter (24904): #41 _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1735:7)
I/flutter (24904): #42 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3955:14)
I/flutter (24904): #43 _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1733:13)
I/flutter (24904): #44 _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1735:7)
I/flutter (24904): #45 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3955:14)
I/flutter (24904): #46 _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1733:13)
I/flutter (24904): #47 _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1735:7)
I/flutter (24904): #48 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3955:14)
I/flutter (24904): #49 _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1733:13)
I/flutter (24904): #50 _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1735:7)
I/flutter (24904): #51 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3955:14)
I/flutter (24904): #52 _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1733:13)
I/flutter (24904): #53 ListIterable.forEach (dart:_internal/iterable.dart:39:13)
I/flutter (24904): #54 _InactiveElements._unmountAll (package:flutter/src/widgets/framework.dart:1746:25)
I/flutter (24904): #55 BuildOwner.finalizeTree.<anonymous closure> (package:flutter/src/widgets/framework.dart:2426:27)
I/flutter (24904): #56 BuildOwner.lockState (package:flutter/src/widgets/framework.dart:2258:15)
I/flutter (24904): #57 BuildOwner.finalizeTree (package:flutter/src/widgets/framework.dart:2425:7)
I/flutter (24904): #58 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:702:18)
I/flutter (24904): #59 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:285:5)
I/flutter (24904): #60 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1016:15)
I/flutter (24904): #61 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:958:9)
I/flutter (24904): #62 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:874:5)
I/flutter (24904): #66 _invoke (dart:ui/hooks.dart:236:10)
I/flutter (24904): #67 _drawFrame (dart:ui/hooks.dart:194:3)
I/flutter (24904): (elided 3 frames from package dart:async)
I/flutter (24904): ════════════════════════════════════════════════════════════════════════════════════════════════════

出现原因

在Drawer的子页面进行全局数据修改报错。比如:在MyApp里面通过全局状态管理主题颜色,然后在设置页面修改主题颜色,就会报这样的错。

Flutter官方说明是因为重绘Scaffold时,需要先关闭Drawer。

解决方案

在跳转侧边栏Drawer的子页面前,先关闭Drawer。

1
2
Application.router.pop(context);  // 先关闭Drawer
Application.router.navigateTo(context, Routers.settingPage);

详情请看。Flutter issue #25601 (comment)

持续更新中…

坚持原创技术分享,您的支持将鼓励我继续创作!