ルートを使ってある画面から別の画面に移動しようとしています。指定されたルートにページを移動するためのボタンを押すと、エラーが発生します。
I/flutter ( 8790): Another exception was thrown: There are multiple heroes that share the same tag within a subtree.
コードは次のとおりです:
ルート:
<String, WidgetBuilder>{
'/first':(BuildContext context) =>NavigatorOne() ,
'/second':(BuildContext context) =>NavigatorTwo(),
'/third':(BuildContext context) =>NavigatorThree(),
},
Navigator.of(context).pushNamed('/first');
Navigator.of(context).pushNamed('/second');
Navigator.of(context).pushNamed('/third');
class NavigatorOne extends StatefulWidget {
@override
_NavigatorOneState createState() => _NavigatorOneState();
}
class _NavigatorOneState extends State<NavigatorOne> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Container(
color: Colors.green,
child: RaisedButton(child: Text(' one 1'),onPressed: (){
Navigator.of(context).pushNamed('/second');
},),
),
);
}
}
そしてエラー:
══╡ EXCEPTION CAUGHT BY SCHEDULER LIBRARY ╞═════════════════════════════════════════════════════════ I/flutter (21786): The following assertion was thrown during a scheduler callback: I/flutter (21786): There are multiple heroes that share the same tag within a subtree. I/flutter (21786): Within each subtree for which heroes are to be animated (typically a PageRoute subtree), each Hero I/flutter (21786): must have a unique non-null tag. I/flutter (21786): In this case, multiple heroes had the following tag: <default FloatingActionButton tag>
これをどうやって解決すればいいでしょうか?
ベストアンサー1
以前にもこの問題に遭遇したことがあり、1 つの画面に 2 つのボタンがあったため、エラーを解消するには、FloatingAction
heroTag プロパティ + 値を追加する必要がありました。FloatingActionButton
例:
FloatingActionButton(
heroTag: "btn1",
...
)
FloatingActionButton(
heroTag: "btn2",
...
)
提供されたサンプル コードからは がないように見えますFloatingActionButton
が、エラーからは が参照されているように見えます。
I/flutter (21786): In this case, multiple heroes had the following tag: default FloatingActionButton tag
おそらく、移動先のページでそれを使用したために、エラーが発生したのでしょう。タグ付きヒーローをプログラム的に作成する場合は、異なるタグを付与する方法を見つける必要があることに注意してください。たとえば、 を作成する場合ListView.builder()
、FloatingActionButtons
文字列フォーマットを使用してタグを渡し、各ボタンに異なるタグが付くようにしてください。例: heroTag: "btn$index"
。