路由问题:“子树中有多个共享相同标签的英雄。”

问题描述 投票:0回答:1
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子树),每个英雄必须具有唯一的非null标签。在这种情况下,多个英雄具有以下标签:A

[这里是令人反感的英雄之一的子树:英雄标签:A状态:_HeroState#d0a27引发异常时,这是堆栈:

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

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

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

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

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

android-studio flutter dart flutter-layout flutter-dependencies
1个回答
0
投票

我已经解决了问题。正如@Jot所说,prodname并非在每种情况下都是唯一的,再加上我使用相同的数据制作了类似的水平列表,因此产生了错误。因此,在输入唯一的产品名称(例如a1,a2或a,b,c)后,该错误已消除。

© www.soinside.com 2019 - 2024. All rights reserved.