【问题标题】:Routing Problem: "There are multiple heroes that share the same tag within a sub-tree ."路由问题:“子树中有多个英雄共享同一个标签。”
【发布时间】:2020-09-14 23:49:03
【问题描述】:
import 'package:flutter/material.dart';
import 'package:ui_mvc/Rings.dart';


class Diamonds extends StatefulWidget {
  @override
  _DiamondsState createState() => _DiamondsState();
}

class _DiamondsState extends State<Diamonds> {

  var dataList= [
    {
     "name": "A1",
      "image":"assets/1.jpg" ,
      "location": "Delhi",
    },
    {
      "name": "A2",
      "image": "assets/2.jpg" ,
      "location": "Delhi",
    },
    {
      "name": "A3",
      "image": "assets/3.jpg" ,
      "location": "Delhi",
    },
    {
      "name": "A4",
      "image": "assets/4.jpg" ,
      "location": "Delhi",
    },
    {
      "name": "A5",
      "image": "assets/5.jpg" ,
      "location": "Delhi",
    },

  ];
  @override
  Widget build(BuildContext context) {
    return GridView.builder(
        scrollDirection: Axis.horizontal,
        itemCount: dataList.length,
        gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 1),
        itemBuilder: (BuildContext context,int index){
          return SingleProd(
            prodName: dataList[index]['name'],
            prodImage: dataList[index]['image'],
            prodLocation: dataList[index]['location'],
          );
        });
  }
}

class SingleProd extends StatelessWidget {
  final prodName;
  final prodImage;
  final prodLocation;

  SingleProd({
   this.prodName,
   this.prodImage,
   this.prodLocation,
});

  @override
  Widget build(BuildContext context) {
    return Card(
      child: Hero(
          tag: prodName,
          child: Material(
            child: InkWell(
              onTap: ()=>
                Navigator.of(context).push(
                  new MaterialPageRoute(builder: (context)=> new Rings())
                ),
              child: GridTile(
                footer: Container(
                  color: Colors.white,
                  child: ListTile(
                    title: Text(
                      prodName,
                      textAlign: TextAlign.left,
                    ),
                    subtitle: Text(
                      prodLocation,
                      textAlign: TextAlign.left,
                    ),
                  ),
                ),
                child: Image.asset(
                    prodImage,
                    fit: BoxFit.fitHeight)
              ),
            ),
          )
      ),
    );
  }
}

所以我试图显示一个包含 5 个图块的水平列表。我希望每个图块都重定向到同一页面(暂时),这就是我调用在另一个页面中定义的 Rings() 的原因。在点击其中一个瓷砖时,屏幕变黑。 但它只是不断向我显示此错误: 在调度程序回调期间引发了以下断言: 子树中有多个英雄共享同一个标签。

在每个要为其动画英雄的子树(即 PageRoute 子树)中,每个英雄必须有一个唯一的非空标记。 在这种情况下,多个英雄具有以下标签:A

这是其中一位违规英雄的子树:Hero 标签:一个 状态:_HeroState#d0a27 抛出异常时,这是堆栈:

0 Hero._allHeroesFor.inviteHero。 (包:flutter/src/widgets/heroes.dart:265:11)

1 Hero._allHeroesFor.inviteHero(包:flutter/src/widgets/heroes.dart:276:8)

2 Hero._allHeroesFor.visitor(包:flutter/src/widgets/heroes.dart:295:21)

3 SingleChildRenderObjectElement.visitChildren(包:flutter/src/widgets/framework.dart:5433:14)

4 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:308:15)

【问题讨论】:

  • 您确定 prodName 是唯一的吗?
  • @Jot 是的,我每次都给不同的名字...... A1 和 A2 算作字符串不同吗?如果是,那么是的,它们是独一无二的
  • 您是否在某处定义了多个FloatingActionButtons?我试过你的示例代码,它对我来说很好。
  • 我的整个项目中没有FloatingActionButton,但是当我从子屏幕导航到父屏幕时仍然出现此错误。请建议我有什么解决方案?非常感谢。

标签: android-studio flutter dart flutter-layout flutter-dependencies


【解决方案1】:

我已经解决了这个问题。正如@Jot 所说,prodname 在每种情况下都不是唯一的,而且我使用相同的数据制作了一个类似的水平列表,因此它产生了一个错误。因此,在输入唯一的产品名称(如 a1、a2 或 a、b、c)后,错误已被删除。

【讨论】:

  • 我的整个项目中没有FloatingActionButton,但是当我从子屏幕导航到父屏幕时仍然出现此错误。请建议我有什么解决方案?非常感谢。
猜你喜欢
  • 1970-01-01
  • 2022-11-16
  • 2021-05-08
  • 2022-11-17
  • 2013-01-28
  • 2013-04-01
  • 2019-04-10
  • 1970-01-01
  • 2013-06-17
相关资源
最近更新 更多