老师你好,我在PhotoApp中build如下代码:
如果 没有 children: _getImages(),就可以运行,添加后会报错
麻烦可以帮我看下为什么会报那个错误么?
代码如下:
return MaterialApp(
title: ‘这是一个相机APP的demo’,
theme: ThemeData(backgroundColor: Colors.white),
home: Scaffold(
appBar: AppBar(
title: Text(‘相机APP的demo’),
leading: GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: Icon(Icons.arrow_back),
),
),
body: Center(
child: Wrap(
spacing: 5,
runSpacing: 5,
children: _getImages(),
)),
floatingActionButton: FloatingActionButton(
onPressed: _pickImage(),
tooltip: ‘选择图片’,
child: Icon(Icons.add_a_photo),
),
),
);
错误如下:
Syncing files to device AOSP on HammerHead…
I/flutter (16585): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (16585): The following assertion was thrown building PhotoApp(dirty, dependencies:
I/flutter (16585): [_LocalizationsScope-[GlobalKey#69ffc], _InheritedTheme], state: _PhotoAppState#21835):
I/flutter (16585): setState() or markNeedsBuild() called during build.
I/flutter (16585): This Overlay widget cannot be marked as needing to build because the framework is already in the
I/flutter (16585): process of building widgets. A widget can be marked as needing to be built during the build phase
I/flutter (16585): only if one of its ancestors is currently building. This exception is allowed because the framework
I/flutter (16585): builds parent widgets before children, which means a dirty descendant will always be built.
I/flutter (16585): Otherwise, the framework might not visit this widget during this build phase.
I/flutter (16585): The widget on which setState() or markNeedsBuild() was called was:
I/flutter (16585): Overlay-[LabeledGlobalKey#bf2f4](state: OverlayState#992e6(tickers: tracking 1
I/flutter (16585): ticker, entries: [OverlayEntry#f93c3(opaque: false; maintainState: false),
I/flutter (16585): OverlayEntry#c4a28(opaque: false; maintainState: true), OverlayEntry#2fddd(opaque: false;
I/flutter (16585): maintainState: false), OverlayEntry#ac5f2(opaque: false; maintainState: true),
I/flutter (16585): OverlayEntry#c9481(opaque: false; maintainState: false), OverlayEntry#d8035(opaque: false;
I/flutter (16585): maintainState: true)]))
I/flutter (16585): The widget which was currently being built when the offending call was made was:
I/flutter (16585): PhotoApp(dirty, dependencies: [_LocalizationsScope-[GlobalKey#69ffc], _InheritedTheme], state:
I/flutter (16585): _PhotoAppState#21835)
I/flutter (16585): When the exception was thrown, this was the stack:
I/flutter (16585): #0 Element.markNeedsBuild. (package:flutter/src/widgets/framework.dart:3497:11)
I/flutter (16585): #1 Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:3523:6)
I/flutter (16585): #2 State.setState (package:flutter/src/widgets/framework.dart:1138:14)
I/flutter (16585): #3 OverlayState.insertAll (package:flutter/src/widgets/overlay.dart:301:5)
I/flutter (16585): #4 OverlayRoute.install (package:flutter/src/widgets/routes.dart:43:24)
I/flutter (16585): #5 TransitionRoute.install (package:flutter/src/widgets/routes.dart:184:11)
I/flutter (16585): #6 ModalRoute.install (package:flutter/src/widgets/routes.dart:899:11)
I/flutter (16585): #7 NavigatorState.push (package:flutter/src/widgets/navigator.dart:1672:11)
I/flutter (16585): #8 Navigator.push (package:flutter/src/widgets/navigator.dart:1011:34)
解锁Flutter开发新姿势,,系统掌握Flutter开发核心技术。
了解课程