导航到其他屏幕时,路线抛出异常“子树中有多个英雄共享同一标签”

时间:2019-12-25 14:37:29

标签: flutter dart dart-pub

Expanded(flex: 1, child: GestureDetector(
               onTap: () {
              Navigator.push(
                  context, MaterialPageRoute(builder: (context) => ResultPage()
              ));
            },
            child: Container(
              alignment: Alignment.center,
              color: Color(0XFFEB1555),
              child: Text("CALCULATE", style: TextStyle(color: Colors.white,
                  fontSize: 24,
                  fontWeight: FontWeight.bold),),
            ),
          ))

获取异常:

  

════════调度程序库捕获了异常   ══════════════════════════════════════════════════ ═══以下   在调度程序回调期间引发了断言:有多个   在子树中共享相同标签的英雄。

     

要在每个子树中对英雄进行动画处理(即   PageRoute子树),每个Hero必须具有唯一的非null标签。在这   案例中,多个英雄具有以下标签:这是其中一个违规行为的子树   英雄:   ................................................... ...........

4 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,解决了删除手势检测器的问题,改用了inkwell onTap。

更改了此内容:

GestureDetector(
      onTap: () {
        Navigator.of(context, rootNavigator: true).push(
          MaterialPageRoute(
            builder: (context) => ChangeNotifierProvider(
              create: (BuildContext context) {
                ProductViewModel();
              },
              child: ProductDetails(
                product: _shownProduct,
              ),
            ),
          ),
        );
      },
      child: InkWell(
            child: Hero(
              tag: _shownProduct.productID,
              child: CachedNetworkImage(
                fadeInDuration: Duration(milliseconds: 1),
                imageUrl: _shownProduct.images[0],
                imageBuilder: (context, imageProvider) => Container(
                  height: MediaQuery.of(context).size.flipped.height / 3.2,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(5),
                    image: DecorationImage(
                      image: imageProvider,
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
                placeholder: (context, url) => CircularProgressIndicator(
                  strokeWidth: 0.5,
                ),
                errorWidget: (context, url, error) => Icon(Icons.error),
              ),
            ),
          ),

对此:

InkWell(
            onTap: () {
              Navigator.of(context).push(
                MaterialPageRoute(
                  builder: (context) => ProductDetails(
                    product: _shownProduct,
                  ),
                ),
              );
            },
            child: Hero(
              tag: _shownProduct.productID,
              child: CachedNetworkImage(
                fadeInDuration: Duration(milliseconds: 1),
                imageUrl: _shownProduct.images[0],
                imageBuilder: (context, imageProvider) => Container(
                  height: MediaQuery.of(context).size.flipped.height / 3.2,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(5),
                    image: DecorationImage(
                      image: imageProvider,
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
                placeholder: (context, url) => CircularProgressIndicator(
                  strokeWidth: 0.5,
                ),
                errorWidget: (context, url, error) => Icon(Icons.error),
              ),
            ),
          ),

它解决了我的问题。

答案 1 :(得分:1)

添加我的解决方案是因为我的问题是由于将少量dart文件移动到不同目录所致。造成了完全相同的问题。

尽管我一直在检查上面给出的解决方案,但是直到我意识到我需要从'import'更改dart文件路径时,它们都无法帮助。 希望这对以后的人有所帮助。

答案 2 :(得分:0)

每个晶圆厂都必须将唯一的英雄密钥取消为空

floatingActionButton: FloatingActionButton(
          heroTag: 'unique_key',
          ...
        ),

和英雄小部件

Hero(
      tag: 'unique_key',
      ...
    ),

答案 3 :(得分:0)

请确保您没有使用 FAB,因为我认为我没有在我的页面上使用 FAB,但经过长时间的分析,我明白我已经在小部件-> 小部件-> 小部件中使用了它,然后如果您想确定唯一键然后你可以使用

FloatingActionButton(
                  heroTag: StringOperations().generateRandomString(5),
                    backgroundColor: MyColors.shineBlueColor,
                    onPressed: () {
                   
                    },
                    label: Text(
                      "text",
                    ),
              
                  )

那么:

     String generateRandomString(int len) {
        var r = Random();
        return String.fromCharCodes(List.generate(len, (index) => r.nextInt(33) + 89));
      }
//will give you random string