Flutter I/flatter(5378):引发了另一个异常:在子树中有多个英雄共享同一标记
我正在尝试从一个屏幕导航到另一个屏幕,当我单击时,出现错误 我已经从互联网和其他网站上找到了一些错误修复,但仍然是错误 这是我的代码:Flutter I/flatter(5378):引发了另一个异常:在子树中有多个英雄共享同一标记,flutter,Flutter,我正在尝试从一个屏幕导航到另一个屏幕,当我单击时,出现错误 我已经从互联网和其他网站上找到了一些错误修复,但仍然是错误 这是我的代码: _buildCard(String title, String rating, String imgPath) { return Padding( padding: EdgeInsets.all(10.0), child: InkWell( onTap: () { Navi
_buildCard(String title, String rating, String imgPath) {
return Padding(
padding: EdgeInsets.all(10.0),
child: InkWell(
onTap: () {
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => DetailPage()));
},
这是我的错误
I/flatter(5378):引发了另一个异常:在子树中有多个英雄共享同一标记
英雄标记必须是唯一的,可能一个图像被渲染多次。您可以使用以下代码生成随机字符串:
import 'dart:math';
String _randomString(int length) {
var rand = new Random();
var codeUnits = new List.generate(
length,
(index){
return rand.nextInt(33)+89;
}
);
return new String.fromCharCodes(codeUnits);
}
英雄标记必须是唯一的,可能一个图像被渲染多次。您可以使用以下代码生成随机字符串:
import 'dart:math';
String _randomString(int length) {
var rand = new Random();
var codeUnits = new List.generate(
length,
(index){
return rand.nextInt(33)+89;
}
);
return new String.fromCharCodes(codeUnits);
}
您的每个heroTag必须有一个唯一值。 在您的情况下,如果您在内部使用hero小部件,请添加heroTag值作为listview索引,该索引将作为唯一值使用。同样地,在下一个屏幕中,将相同的索引传递给相应的hero小部件,您要在其中转换您的hero小部件 例如: //在发送屏幕上
onTap: () {
BuildContext context;
Navigator.of(context).push(MaterialPageRoute(
builder: (context) =>
DetailsIcon(heroTag: index.toString(), iconName: iconName)));
},
class DetailsIconextends StatelessWidget {
DetailsIcon({this.heroTag,this.iconName}) : super(key: key);
final String photo;
final String onTap;
Widget build(BuildContext context) {
return SizedBox(
width: width,
child: Hero(
tag: heroTag,
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: onTap,
child: Image.asset(
iconName,
fit: BoxFit.contain,
),
),
),
),
);
}
}
//在接收屏幕上
onTap: () {
BuildContext context;
Navigator.of(context).push(MaterialPageRoute(
builder: (context) =>
DetailsIcon(heroTag: index.toString(), iconName: iconName)));
},
class DetailsIconextends StatelessWidget {
DetailsIcon({this.heroTag,this.iconName}) : super(key: key);
final String photo;
final String onTap;
Widget build(BuildContext context) {
return SizedBox(
width: width,
child: Hero(
tag: heroTag,
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: onTap,
child: Image.asset(
iconName,
fit: BoxFit.contain,
),
),
),
),
);
}
}
此外,如果您不是只使用一个小部件而不是小部件列表,那么您可以只向两个屏幕添加一个公共值来进行转换
这将帮助您解决您的问题
如需更多参考,请访问
我希望这会有所帮助。您的每个heroTag必须具有唯一值。 在您的情况下,如果您在内部使用hero小部件,请添加heroTag值作为listview索引,该索引将作为唯一值使用。同样地,在下一个屏幕中,将相同的索引传递给相应的hero小部件,您要在其中转换您的hero小部件 例如: //在发送屏幕上
onTap: () {
BuildContext context;
Navigator.of(context).push(MaterialPageRoute(
builder: (context) =>
DetailsIcon(heroTag: index.toString(), iconName: iconName)));
},
class DetailsIconextends StatelessWidget {
DetailsIcon({this.heroTag,this.iconName}) : super(key: key);
final String photo;
final String onTap;
Widget build(BuildContext context) {
return SizedBox(
width: width,
child: Hero(
tag: heroTag,
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: onTap,
child: Image.asset(
iconName,
fit: BoxFit.contain,
),
),
),
),
);
}
}
//在接收屏幕上
onTap: () {
BuildContext context;
Navigator.of(context).push(MaterialPageRoute(
builder: (context) =>
DetailsIcon(heroTag: index.toString(), iconName: iconName)));
},
class DetailsIconextends StatelessWidget {
DetailsIcon({this.heroTag,this.iconName}) : super(key: key);
final String photo;
final String onTap;
Widget build(BuildContext context) {
return SizedBox(
width: width,
child: Hero(
tag: heroTag,
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: onTap,
child: Image.asset(
iconName,
fit: BoxFit.contain,
),
),
),
),
);
}
}
此外,如果您不是只使用一个小部件而不是小部件列表,那么您可以只向两个屏幕添加一个公共值来进行转换
这将帮助您解决您的问题
如需更多参考,请访问
我希望这能有所帮助。您能提供完整的代码吗?请共享两页代码。这是我在Github上的完整代码。您能为我们提供完整的代码吗?请共享两页代码。这是我在github上的完整代码