我正在构建一个应用程序,其中Home ui看起来像网格,当在网格用户的单个图块上点击时,将导航到另一个页面。但是无论何时发生错误都会引发。这是不考虑语法错误的代码。 我没有使用任何英雄部件。虽然我收到此错误。.
body: Container(
child: GridView.count(
crossAxisCount: 2,
children: <Widget>[
Card(
child: InkWell(
onTap: (){
print('tapped pnemonia');
Navigator.push(context, MaterialPageRoute(builder: (context)=> diagnosis()));
},
child: Center(
child: Column(
children: <Widget>[
Text('Pnemonia'),
Image.asset('assets/images/lungs.jpg'),
],
),
),
),
),
],
),
),
);
}
}
,输出错误如下:
I/flutter ( 6827): ├# Here is the subtree for one of the offending heroes: Hero
I/flutter ( 6827):
I/flutter ( 6827): When the exception was thrown, this was the stack:
I/flutter ( 6827): #0 Hero._allHeroesFor.inviteHero.<anonymous closure> (package:flutter/src/widgets/heroes.dart:265:11)
I/flutter ( 6827): #1 Hero._allHeroesFor.inviteHero (package:flutter/src/widgets/heroes.dart:276:8)
I/flutter ( 6827): #2 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:295:21)
I/flutter ( 6827): #3 SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:5433:14)
I/flutter ( 6827): #4 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): #5 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4272:14)
I/flutter ( 6827): #6 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): #7 SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:5433:14)
I/flutter ( 6827): #8 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): #9 MultiChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:5534:16)
I/flutter ( 6827): #10 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): #11 SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:5433:14)
I/flutter ( 6827): #12 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): #13 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4272:14)
I/flutter ( 6827): #14 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): #15 SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:5433:14)
I/flutter ( 6827): #16 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): #17 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4272:14)
I/flutter ( 6827): #18 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): #19 MultiChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:5534:16)
I/flutter ( 6827): #20 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): #21 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4272:14)
I/flutter ( 6827): #22 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): #23 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4272:14)
I/flutter ( 6827): #24 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): #25 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4272:14)
I/flutter ( 6827): #26 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): #27 MultiChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:5534:16)
I/flutter ( 6827): #28 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): #29 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4272:14)
I/flutter ( 6827): #30 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): #31 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4272:14)
I/flutter ( 6827): #32 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): #33 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4272:14)
I/flutter ( 6827): #34 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): #35 SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:5433:14)
I/flutter ( 6827): #36 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): #37 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4272:14)
I/flutter ( 6827): #38 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): #39 SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:5433:14)
I/flutter ( 6827): #40 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): #41 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4272:14)
I/flutter ( 6827): #42 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): #43 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4272:14)
I/flutter ( 6827): #44 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): #45 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4272:14)
I/flutter ( 6827): #46 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): #47 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4272:14)
I/flutter ( 6827): #48 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)
I/flutter ( 6827): (elided 3 frames from package dart:async)
I/flutter ( 6827):
I/flutter ( 6827): Another exception was thrown: There are multiple heroes that share the same tag within a subtree.
这是我运行版本时出现的错误。
答案 0 :(得分:3)
可能已经晚了,但是,如果您仍然感到困惑。我通过在FloatingActionButtons中手动添加新属性heroTag
解决了我的问题。
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FloatingActionButton(
heroTag: 1, // put something different with this
onPressed: (){
counterBloc.add(CounterEvent.increment);
},
child: Icon(Icons.add),
),
FloatingActionButton(
heroTag: 2, // put something different with this
onPressed: (){
counterBloc.add(CounterEvent.decrement);
},
child: Icon(Icons.remove),
),
],
);
}
希望对您有所帮助
答案 1 :(得分:1)
当您尝试在一个小部件中使用多个multiple heroes on one route can't have the same tag
时,可能会发生错误FloatingActionButtons
。
根据heroTag
中FloatingActionButton
的文档:
应用于按钮的Hero小部件的标签。
默认为与其他浮动操作按钮匹配的标签。
如果您不想执行浮动操作,请将其显式设置为null 按钮以具有英雄标签。
如果未明确设置,则只能有一个 每条路线(即每个屏幕)的FloatingActionButton,否则 会有标签冲突(一条路线上的多个英雄不能有 相同的标签)。材料设计规范建议仅使用 每个屏幕一个浮动操作按钮。
要解决您的异常并在单个窗口小部件中使用多个FAB,请确保在FAB中为Unique
传递heroTag
对象/值/字符串。
注意:如果不将值传递给heroTag
,它将创建默认值<default FloatingActionButton tag>
。这对于所有FAB都是通用的。因此,如果它们在同一个小部件中,则将使用重复的heroTag
并抛出异常。
如果您有任何疑问,请告诉我。