gpt4 book ai didi

exception - Flutter:有 2 个 FloatingActionButton 时出现异常

转载 作者:IT王子 更新时间:2023-10-29 06:35:17 24 4
gpt4 key购买 nike

当此构建涉及 2 个 FloatingActionButton 时,我第一次构建页面时收到异常。

但是,如果我注释掉 2 个 FloatingActionButton 之一,运行该应用程序并在稍后阶段取消注释并继续进行热重载,它会起作用。

以下代码实现了一个图像选择器,并让用户可以选择从图库或相机中进行选择。

有人可以看看这个并告诉我我做错了什么吗?

我已经突出显示了导致异常的代码部分。

代码如下:

class ProfilePage extends StatefulWidget {
ProfilePage(this.profileId);
final int profileId;
@override
_ProfilePageState createState() => new _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> {
var _imageInfo;

@override
void initState() {
_imageInfo = mid.currentProfile['Photo'];
super.initState();
}

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(Translations
.of(context)
.text(widget.profileId == -1 ? 'profiles_add' : 'profiles_edit')),
actions: <Widget>[
new IconButton(
icon: new Icon(Icons.close),
onPressed: () => Navigator.of(context).pop(null),
),
],
leading: new Container(),
),
body: _build_body(),
);
}

_build_body() {
var profileImage;
profileImage = new Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
new GestureDetector(
child: new Container(
child: new ProfilePicture(imageInfo: _imageInfo),
width: 100.0,
height: 100.0,
alignment: Alignment.center,
),
onTap: () {},
),
// ##################################
new FloatingActionButton(
onPressed: () {
getImage(ImageSource.camera);
},
tooltip: 'Take a Photo',
child: new Icon(Icons.camera_alt)
),
new FloatingActionButton(
onPressed: () {
getImage(ImageSource.gallery);
},
tooltip: 'Pick Image from gallery',
child: new Icon(Icons.photo_library)
),
// ##################################
],
),
);

return new Container(
child: new Column(
children: [profileImage],
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
),
);
}

Future<dynamic> getImage(ImageSource source) async {
var imageFile = await ImagePicker.pickImage(source: source);

if (imageFile != null) {
List<int> imageBytes = imageFile.readAsBytesSync();
String base64Image = base64.encode(imageBytes);

setState(() {
_imageInfo = "base64:" + base64Image;
});
}
}
}

异常(exception)情况:

I/flutter ( 9045): ══╡ EXCEPTION CAUGHT BY SCHEDULER LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 9045): The following assertion was thrown during a scheduler callback:
I/flutter ( 9045): There are multiple heroes that share the same tag within a subtree.
I/flutter ( 9045): Within each subtree for which heroes are to be animated (typically a PageRoute subtree), each Hero
I/flutter ( 9045): must have a unique non-null tag.
I/flutter ( 9045): In this case, multiple heroes had the following tag: <default FloatingActionButton tag>
I/flutter ( 9045): Here is the subtree for one of the offending heroes:
I/flutter ( 9045): # Hero(tag: <default FloatingActionButton tag>, state: _HeroState#5ea48)
I/flutter ( 9045): # └KeyedSubtree-[GlobalKey#d064f]
I/flutter ( 9045): # └RawMaterialButton(state: _RawMaterialButtonState#8f3c6)
I/flutter ( 9045): # └Semantics(container: true, properties: SemanticsProperties, label: null, value: null, hint: null, renderObject: RenderSemanticsAnnotations#d7c9b relayoutBoundary=up5 NEEDS-PAINT)
I/flutter ( 9045): # └ConstrainedBox(BoxConstraints(w=56.0, h=56.0), renderObject: RenderConstrainedBox#8437c relayoutBoundary=up6 NEEDS-PAINT)
I/flutter ( 9045): # └Material(type: button, elevation: 6.0, color: Color(0xff2196f3), textStyle.debugLabel: ((englishLike button).merge(whiteMountainView button)).copyWith, textStyle.inherit: false, textStyle.color: Color(0xffffffff), textStyle.family: Roboto, textStyle.size: 14.0, textStyle.weight: 500, textStyle.letterSpacing: 1.2, textStyle.baseline: alphabetic, textStyle.decoration: TextDecoration.none, shape: CircleBorder(BorderSide(Color(0xff000000), 0.0, BorderStyle.none)), state: _MaterialState#73b5e)
I/flutter ( 9045): # └_MaterialInterior(duration: 200ms, shape: CircleBorder(BorderSide(Color(0xff000000), 0.0, BorderStyle.none)), elevation: 6.0, color: Color(0xff2196f3), shadowColor: Color(0xff000000), state: _MaterialInteriorState#aa4b9(ticker inactive))
I/flutter ( 9045): # └PhysicalShape(clipper: ShapeBorderClipper, elevation: 6.0, color: Color(0xff2196f3), shadowColor: Color(0xff000000), renderObject: RenderPhysicalShape#0486d NEEDS-PAINT)
I/flutter ( 9045): # └_ShapeBorderPaint
I/flutter ( 9045): # └CustomPaint(renderObject: RenderCustomPaint#7e004 NEEDS-PAINT)
I/flutter ( 9045): # └NotificationListener<LayoutChangedNotification>
I/flutter ( 9045): # └_InkFeatures-[GlobalKey#f1e6b ink renderer](renderObject: _RenderInkFeatures#34dbd NEEDS-PAINT)
I/flutter ( 9045): # └AnimatedDefaultTextStyle(duration: 200ms, debugLabel: ((englishLike button).merge(whiteMountainView button)).copyWith, inherit: false, color: Color(0xffffffff), family: Roboto, size: 14.0, weight: 500, letterSpacing: 1.2, baseline: alphabetic, decoration: TextDecoration.none, softWrap: wrapping at box width, overflow: clip, state: _AnimatedDefaultTextStyleState#c8a96(ticker inactive))
I/flutter ( 9045): # └DefaultTextStyle(debugLabel: ((englishLike button).merge(whiteMountainView button)).copyWith, inherit: false, color: Color(0xffffffff), family: Roboto, size: 14.0, weight: 500, letterSpacing: 1.2, baseline: alphabetic, decoration: TextDecoration.none, softWrap: wrapping at box width, overflow: clip)
I/flutter ( 9045): # └InkWell(gestures: [tap], clipped to BoxShape.rectangle, state: _InkResponseState<InkResponse>#32f3c)
I/flutter ( 9045): # └GestureDetector
I/flutter ( 9045): # └RawGestureDetector(state: RawGestureDetectorState#d6211(gestures: [tap], behavior: opaque))
I/flutter ( 9045): # └_GestureSemantics(renderObject: RenderSemanticsGestureHandler#d616c NEEDS-PAINT)
I/flutter ( 9045): # └Listener(listeners: [down], behavior: opaque, renderObject: RenderPointerListener#8a585 NEEDS-PAINT)
I/flutter ( 9045): # └Builder
I/flutter ( 9045): # └IconTheme(IconThemeData#15fa8(color: Color(0xffffffff)))
I/flutter ( 9045): # └Container(padding: EdgeInsets.zero)
I/flutter ( 9045): # └Padding(padding: EdgeInsets.zero, renderObject: RenderPadding#45f72 NEEDS-PAINT)
I/flutter ( 9045): # └Center(alignment: center, widthFactor: 1.0, heightFactor: 1.0, renderObject: RenderPositionedBox#a4315 NEEDS-PAINT)
I/flutter ( 9045): # └SizedBox.expand(renderObject: RenderConstrainedBox#af3fc relayoutBoundary=up1 NEEDS-PAINT)
I/flutter ( 9045): # └Tooltip("Pick Image from gallery", vertical offset: 24.0, position: below, state: _TooltipState#36588(ticker inactive))
I/flutter ( 9045): # └GestureDetector
I/flutter ( 9045): # └RawGestureDetector(state: RawGestureDetectorState#6cd1d(gestures: [long press], behavior: opaque))
I/flutter ( 9045): # └Listener(listeners: [down], behavior: opaque, renderObject: RenderPointerListener#aae3f NEEDS-PAINT)
I/flutter ( 9045): # └Semantics(container: false, properties: SemanticsProperties, label: "Pick Image from gallery", value: null, hint: null, renderObject: RenderSemanticsAnnotations#5fa0a NEEDS-PAINT)
I/flutter ( 9045): # └Builder
I/flutter ( 9045): # └IconTheme(IconThemeData#15fa8(color: Color(0xffffffff)))
I/flutter ( 9045): # └Icon(IconData(U+0E413))
I/flutter ( 9045): # └Semantics(container: false, properties: SemanticsProperties, label: null, value: null, hint: null, renderObject: RenderSemanticsAnnotations#8a909 NEEDS-PAINT)
I/flutter ( 9045): # └ExcludeSemantics(excluding: true, renderObject: RenderExcludeSemantics#fc581 NEEDS-PAINT)
I/flutter ( 9045): # └SizedBox(width: 24.0, height: 24.0, renderObject: RenderConstrainedBox#f68c4 NEEDS-PAINT)
I/flutter ( 9045): # └Center(alignment: center, renderObject: RenderPositionedBox#c38ac NEEDS-PAINT)
I/flutter ( 9045): # └RichText(textDirection: ltr, softWrap: wrapping at box width, maxLines: unlimited, text: "", renderObject: RenderParagraph#8873e relayoutBoundary=up1 NEEDS-PAINT)
I/flutter ( 9045):
I/flutter ( 9045): When the exception was thrown, this was the stack:
I/flutter ( 9045): #0 Hero._allHeroesFor.visitor.<anonymous closure> (package:flutter/src/widgets/heroes.dart:130:13)
I/flutter ( 9045): #1 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:140:10)
I/flutter ( 9045): #2 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3659:14)
I/flutter ( 9045): #3 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:144:15)
I/flutter ( 9045): #4 SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:4625:14)
I/flutter ( 9045): #5 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:144:15)
I/flutter ( 9045): #6 MultiChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:4725:16)
I/flutter ( 9045): #7 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:144:15)
I/flutter ( 9045): #8 SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:4625:14)
I/flutter ( 9045): #9 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:144:15)
I/flutter ( 9045): #10 MultiChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:4725:16)
I/flutter ( 9045): #11 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:144:15)
I/flutter ( 9045): #12 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3659:14)
I/flutter ( 9045): #13 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:144:15)
I/flutter ( 9045): #14 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3659:14)
I/flutter ( 9045): #15 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:144:15)
I/flutter ( 9045): #16 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3659:14)
I/flutter ( 9045): #17 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:144:15)
I/flutter ( 9045): #18 MultiChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:4725:16)
I/flutter ( 9045): #19 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:144:15)
I/flutter ( 9045): #20 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3659:14)
I/flutter ( 9045): #21 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:144:15)
I/flutter ( 9045): #22 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3659:14)
I/flutter ( 9045): #23 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:144:15)
I/flutter ( 9045): #24 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3659:14)
I/flutter ( 9045): #25 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:144:15)
I/flutter ( 9045): #26 SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:4625:14)
I/flutter ( 9045): #27 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:144:15)
I/flutter ( 9045): #28 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3659:14)
I/flutter ( 9045): #29 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:144:15)
I/flutter ( 9045): #30 SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:4625:14)
I/flutter ( 9045): #31 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:144:15)
I/flutter ( 9045): #32 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3659:14)
I/flutter ( 9045): #33 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:144:15)
I/flutter ( 9045): #34 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3659:14)
I/flutter ( 9045): #35 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:144:15)
I/flutter ( 9045): #36 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3659:14)
I/flutter ( 9045): #37 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:144:15)
I/flutter ( 9045): #38 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3659:14)
I/flutter ( 9045): #39 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:144:15)
I/flutter ( 9045): #40 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3659:14)
I/flutter ( 9045): #41 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:144:15)
I/flutter ( 9045): #42 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3659:14)
I/flutter ( 9045): #43 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:144:15)
I/flutter ( 9045): #44 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3659:14)
I/flutter ( 9045): #45 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:144:15)
I/flutter ( 9045): #46 SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:4625:14)
I/flutter ( 9045): #47 Element.visitChildElements (package:flutter/src/widgets/framework.dart:2625:5)
I/flutter ( 9045): #48 Hero._allHeroesFor (package:flutter/src/widgets/heroes.dart:146:13)
I/flutter ( 9045): #49 HeroController._startHeroTransition (package:flutter/src/widgets/heroes.dart:511:51)
I/flutter ( 9045): #50 HeroController._maybeStartHeroTransition.<anonymous closure> (package:flutter/src/widgets/heroes.dart:492:9)
I/flutter ( 9045): #51 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:990:15)
I/flutter ( 9045): #52 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:938:9)
I/flutter ( 9045): #53 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:842:5)
I/flutter ( 9045): #54 _invoke (dart:ui/hooks.dart:120:13)
I/flutter ( 9045): #55 _drawFrame (dart:ui/hooks.dart:109:3)
I/flutter ( 9045): ════════════════════════════════════════════════════════════════════════════════════════════════════

最佳答案

好的,我发现将 heroTag: null 添加到 FloatingActionButton 的属性中可以解决问题。

导致异常的代码部分现在如下并且似乎可以正常工作:

      new FloatingActionButton(
onPressed: () {
getImage(ImageSource.camera);
},
tooltip: 'Take a Photo',
child: new Icon(Icons.camera_alt),
heroTag: null,
),

new FloatingActionButton(
onPressed: () {
getImage(ImageSource.gallery);
},
tooltip: 'Pick Image from gallery',
child: new Icon(Icons.photo_library),
heroTag: null,
),

关于exception - Flutter:有 2 个 FloatingActionButton 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49964192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com