拍照app开发,上面是代码,下面是运行报错
来源:6-16 拍照APP开发-图片获取与图片展示【实战尝鲜】
newBullet
2019-05-04
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:image_picker/image_picker.dart';
//拍照app开发
void main() => runApp(PhotoApp());
class PhotoApp extends StatefulWidget {
@override
_PhotoAppState createState() => _PhotoAppState();
}
class _PhotoAppState extends State<PhotoApp> {
File _image;
Future getImage(bool isTakePhoto) async {
var image = await ImagePicker.pickImage(source: isTakePhoto?ImageSource.camera:ImageSource.gallery);
setState(() {
_image = image;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('拍照app开发'),
leading: GestureDetector(
onTap: (){
Navigator.pop(context);
},
child: Icon(Icons.arrow_back),
)
),
body: Center(
child: _image == null
? Text('No image selected.')
: Image.file(_image),
),
floatingActionButton: FloatingActionButton(
onPressed: _pickImage(),
tooltip: '选择图片',
child: Icon(Icons.add_a_photo),
),
);
}
_pickImage() {
showModalBottomSheet(context: context, builder: (context)=>Container(
height: 160,
child: Column(
children: <Widget>[
_item('拍照', true),
_item('从相册选择', false),
],
),
));
}
_item(String title, bool isTakePhoto) {
return GestureDetector(
child: ListTile(
leading: Icon(isTakePhoto?Icons.camera_alt:Icons.photo_library),
title: Text(title),
onTap: ()=>getImage(isTakePhoto),
),
);
}
}
Performing hot restart…
Syncing files to device Android SDK built for x86…
Restarted application in 4,098ms.
I/flutter (10962): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════
I/flutter (10962): The following assertion was thrown building
PhotoApp(dirty, dependencies:
I/flutter (10962): [_LocalizationsScope-[GlobalKey#06207],
_InheritedTheme], state: _PhotoAppState#c3f3f):
I/flutter (10962): setState() or markNeedsBuild() called during build.
I/flutter (10962): This Overlay widget cannot be marked as needing to
build because the framework is already in the
I/flutter (10962): process of building widgets. A widget can be marked
as needing to be built during the build phase
I/flutter (10962): only if one of its ancestors is currently building.
This exception is allowed because the framework
I/flutter (10962): builds parent widgets before children, which means
a dirty descendant will always be built.
I/flutter (10962): Otherwise, the framework might not visit this
widget during this build phase.
I/flutter (10962): The widget on which setState() or markNeedsBuild()
was called was:
I/flutter (10962):
Overlay-[LabeledGlobalKey#e55e4](state:
OverlayState#a1f09(tickers: tracking 1
I/flutter (10962): ticker, entries: [OverlayEntry#520cd(opaque:
false; maintainState: false),
I/flutter (10962): OverlayEntry#00833(opaque: false; maintainState:
true), OverlayEntry#a478a(opaque: false;
I/flutter (10962): maintainState: false), OverlayEntry#b1f5b(opaque:
false; maintainState: true),
I/flutter (10962): OverlayEntry#d4962(opaque: false; maintainState:
false), OverlayEntry#ab71d(opaque: false;
I/flutter (10962): maintainState: true)]))
I/flutter (10962): The widget which was currently being built when the
offending call was made was:
I/flutter (10962): PhotoApp(dirty, dependencies:
[_LocalizationsScope-[GlobalKey#06207], _InheritedTheme], state:
I/flutter (10962): _PhotoAppState#c3f3f)
I/flutter (10962):
I/flutter (10962): When the exception was thrown, this was the stack:
I/flutter (10962): #0 Element.markNeedsBuild.
(package:flutter/src/widgets/framework.dart:3497:11)
I/flutter (10962): #1 Element.markNeedsBuild
(package:flutter/src/widgets/framework.dart:3523:6)
I/flutter (10962): #2 State.setState
(package:flutter/src/widgets/framework.dart:1138:14)
I/flutter (10962): #3 OverlayState.insertAll
(package:flutter/src/widgets/overlay.dart:301:5)
I/flutter (10962): #4 OverlayRoute.install
(package:flutter/src/widgets/routes.dart:43:24)
I/flutter (10962): #5 TransitionRoute.install
(package:flutter/src/widgets/routes.dart:184:11)
I/flutter (10962): #6 ModalRoute.install
(package:flutter/src/widgets/routes.dart:899:11)
I/flutter (10962): #7 NavigatorState.push
(package:flutter/src/widgets/navigator.dart:1672:11)
I/flutter (10962): #8 Navigator.push
(package:flutter/src/widgets/navigator.dart:1011:34)
I/flutter (10962): #9 showModalBottomSheet
(package:flutter/src/material/bottom_sheet.dart:332:20)
I/flutter (10962): #10 _PhotoAppState._pickImage
(package:flutter_app/photo_app.dart:53:7)
I/flutter (10962): #11 _PhotoAppState.build
(package:flutter_app/photo_app.dart:45:20)
I/flutter (10962): #12 StatefulElement.build
(package:flutter/src/widgets/framework.dart:3825:27)
I/flutter (10962): #13 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:3736:15)
I/flutter (10962): #14 Element.rebuild
(package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (10962): #15 ComponentElement._firstBuild
(package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (10962): #16 StatefulElement._firstBuild
(package:flutter/src/widgets/framework.dart:3864:11)
I/flutter (10962): #17 ComponentElement.mount
(package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (10962): #18 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (10962): #19 Element.updateChild
(package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (10962): #20 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:4876:14)
I/flutter (10962): #21 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (10962): #22 Element.updateChild
(package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (10962): #23 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (10962): #24 Element.rebuild
(package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (10962): #25 ComponentElement._firstBuild
(package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (10962): #26 ComponentElement.mount
(package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (10962): #27 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (10962): #28 Element.updateChild
(package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (10962): #29 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:4876:14)
I/flutter (10962): #30 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (10962): #31 Element.updateChild
(package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (10962): #32 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:4876:14)
I/flutter (10962): #33 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (10962): #34 Element.updateChild
(package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (10962): #35 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:4876:14)
I/flutter (10962): #36 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (10962): #37 Element.updateChild
(package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (10962): #38 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:4876:14)
I/flutter (10962): #39 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (10962): #40 Element.updateChild
(package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (10962): #41 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (10962): #42 Element.rebuild
(package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (10962): #43 ComponentElement._firstBuild
(package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (10962): #44 StatefulElement._firstBuild
(package:flutter/src/widgets/framework.dart:3864:11)
I/flutter (10962): #45 ComponentElement.mount
(package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (10962): #46 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (10962): #47 Element.updateChild
(package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (10962): #48 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (10962): #49 Element.rebuild
(package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (10962): #50 ComponentElement._firstBuild
(package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (10962): #51 ComponentElement.mount
(package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (10962): #52 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (10962): #53 Element.updateChild
(package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (10962): #54 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (10962): #55 Element.rebuild
(package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (10962): #56 ComponentElement._firstBuild
(package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (10962): #57 StatefulElement._firstBuild
(package:flutter/src/widgets/framework.dart:3864:11)
I/flutter (10962): #58 ComponentElement.mount
(package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (10962): #59 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (10962): #60 Element.updateChild
(package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (10962): #61 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:4876:14)
I/flutter (10962): #62 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (10962): #63 Element.updateChild
(package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (10962): #64 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (10962): #65 Element.rebuild
(package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (10962): #66 ComponentElement._firstBuild
(package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (10962): #67 ComponentElement.mount
(package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (10962): #68 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (10962): #69 Element.updateChild
(package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (10962): #70 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:4876:14)
I/flutter (10962): #71 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (10962): #72 Element.updateChild
(package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (10962): #73 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (10962): #74 Element.rebuild
(package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (10962): #75 ComponentElement._firstBuild
(package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (10962): #76 StatefulElement._firstBuild
(package:flutter/src/widgets/framework.dart:3864:11)
I/flutter (10962): #77 ComponentElement.mount
(package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (10962): #78 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (10962): #79 Element.updateChild
(package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (10962): #80 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (10962): #81 Element.rebuild
(package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (10962): #82 ComponentElement._firstBuild
(package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (10962): #83 ComponentElement.mount
(package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (10962): #84 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (10962): #85 Element.updateChild
(package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (10962): #86 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:4876:14)
I/flutter (10962): #87 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (10962): #88 Element.updateChild
(package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (10962): #89 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (10962): #90 Element.rebuild
(package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (10962): #91 ComponentElement._firstBuild
(package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (10962): #92 ComponentElement.mount
(package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (10962): #93 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (10962): #94 Element.updateChild
(package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (10962): #95 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (10962): #96 Element.rebuild
(package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (10962): #97 ComponentElement._firstBuild
(package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (10962): #98 StatefulElement._firstBuild
(package:flutter/src/widgets/framework.dart:3864:11)
I/flutter (10962): #99 ComponentElement.mount
(package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (10962): #100 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (10962): #101 Element.updateChild
(package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (10962): #102 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (10962): #103 Element.rebuild
(package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (10962): #104 ComponentElement._firstBuild
(package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (10962): #105 StatefulElement._firstBuild
(package:flutter/src/widgets/framework.dart:3864:11)
I/flutter (10962): #106 ComponentElement.mount
(package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (10962): #107 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (10962): #108 Element.updateChild
(package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (10962): #109 RenderObjectElement.updateChildren
(package:flutter/src/widgets/framework.dart:4659:32)
I/flutter (10962): #110 MultiChildRenderObjectElement.update
(package:flutter/src/widgets/framework.dart:4992:17)
I/flutter (10962): #111 Element.updateChild
(package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (10962): #112 _TheatreElement.update
(package:flutter/src/widgets/overlay.dart:508:16)
I/flutter (10962): #113 Element.updateChild
(package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (10962): #114 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (10962): #115 Element.rebuild
(package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (10962): #116 StatefulElement.update
(package:flutter/src/widgets/framework.dart:3894:5)
I/flutter (10962): #117 Element.updateChild
(package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (10962): #118 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (10962): #119 Element.rebuild
(package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (10962): #120 ProxyElement.update
(package:flutter/src/widgets/framework.dart:4006:5)
I/flutter (10962): #121 Element.updateChild
(package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (10962): #122 SingleChildRenderObjectElement.update
(package:flutter/src/widgets/framework.dart:4883:14)
I/flutter (10962): #123 Element.updateChild
(package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (10962): #124 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (10962): #125 Element.rebuild
(package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (10962): #126 StatefulElement.update
(package:flutter/src/widgets/framework.dart:3894:5)
I/flutter (10962): #127 Element.updateChild
(package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (10962): #128 SingleChildRenderObjectElement.update
(package:flutter/src/widgets/framework.dart:4883:14)
I/flutter (10962): #129 Element.updateChild
(package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (10962): #130 SingleChildRenderObjectElement.update
(package:flutter/src/widgets/framework.dart:4883:14)
I/flutter (10962): #131 Element.updateChild
(package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (10962): #132 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (10962): #133 Element.rebuild
(package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (10962): #134 BuildOwner.buildScope
(package:flutter/src/widgets/framework.dart:2273:33)
I/flutter (10962): #135
_WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame
(package:flutter/src/widgets/binding.dart:700:20)
I/flutter (10962): #136
_WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback
(package:flutter/src/rendering/binding.dart:268:5)
I/flutter (10962): #137
_WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback
(package:flutter/src/scheduler/binding.dart:988:15)
I/flutter (10962): #138
_WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame
(package:flutter/src/scheduler/binding.dart:928:9)
I/flutter (10962): #139
_WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame
(package:flutter/src/scheduler/binding.dart:840:5)
I/flutter (10962): #143 _invoke (dart:ui/hooks.dart:209:10)
I/flutter (10962): #144 _drawFrame (dart:ui/hooks.dart:168:3)
I/flutter (10962): (elided 3 frames from package dart:async)
I/flutter (10962):
════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter (10962): Another exception was thrown: NoSuchMethodError:
The method ‘drive’ was called on null.
I/flutter (10962): Another exception was thrown:
‘package:flutter/src/widgets/transitions.dart’: Failed assertion: line
831 pos 15: ‘animation != null’: is not true.
2回答
-
_pickImage使用方式不对要改成
onPressed:_pickImage(), //改成 onPressed:_pickImage,
onPressed:_pickImage(),表示直接调用,是将_pickImage()结果赋值给了onPressed是不对的哦
012019-05-12 -
newBullet
提问者
2019-05-04
注释到这个就不会报错,showModalBottomSheet,不知道为什么
012019-05-05
相似问题