【问题标题】:There are multiple heroes that share the same tag within a subtree error without hero or FloatingActionButton widgets in code在代码中没有 hero 或 FloatingActionButton 小部件的子树错误中有多个英雄共享相同的标签
【发布时间】:2021-05-08 08:19:32
【问题描述】:

我正在开发应用程序,我正在使用路由浏览页面,我没有使用任何英雄或 FloatingActionButtons,但我收到了这个与英雄相关的错误,有没有人知道可能是什么原因那个?

这是导航调用

nextTapped()async{
    if(occasionTitleTxtField.text.isEmpty || dateTxtField.text.isEmpty || detailsTxtField.text.isEmpty)
      ErrorOverlay("Invalid Data", false);
    else
      {
        await LoadingOverlay.of(context).during(UserOperations().createFirstOccasion(context, occasionTitleTxtField.text, dateTxtField.text, detailsTxtField.text, repeatEveryYear, timeLimit)).then((value){
          if(value.elementAt(0) == "true")
              Navigator.of(context).pushReplacementNamed('/firstGift',arguments: FirstGiftPage(value.elementAt(1),occasionTitleTxtField.text));
        });
      }
  }

这里是 route_generator

class RouteGenerator {
  static Route<dynamic> generateRoute(RouteSettings settings) {
    // Getting arguments passed in while calling Navigator.pushNamed
    final args = settings.arguments;

    switch (settings.name) {
      case '/':
        return MaterialPageRoute(builder: (_) => SplashScreen());
      case '/login':
        return MaterialPageRoute(builder: (_) => LoginPage());
      case '/homepage':
        return MaterialPageRoute(builder: (_) => HomePage());
      case '/signUp':
        return MaterialPageRoute(builder: (_) => SignUpScreen());
      case '/otpScreen':
        return MaterialPageRoute(builder: (_) {
          OTPScreen screenArgs = args;
          return OTPScreen(screenArgs.phoneNumber,screenArgs.countryCode);
        });
      case '/phoneNumber':
        return MaterialPageRoute(builder: (_) {
          PhoneNumberScreen screenArgs = args;
          return PhoneNumberScreen(screenArgs.Provider);
        });
      case '/interests':
        return MaterialPageRoute(builder: (_) => InterestsPage());
      case '/invitation':
        return MaterialPageRoute(builder: (_) => InvitationPage());
      case '/firstOccasion':
        return MaterialPageRoute(builder: (_) => FirstOccasion());
      case '/firstGift':
        return MaterialPageRoute(builder: (_) {
          FirstGiftPage screenArgs = args;
          return FirstGiftPage(screenArgs.occasionID,screenArgs.occasionTitle);
        });
      case '/map':
        return MaterialPageRoute(builder: (_) => MapsPage());
      default:
        return _errorRoute();
    }
  }

  static Route<dynamic> _errorRoute() {
    return MaterialPageRoute(builder: (_) {
      return Scaffold(
        appBar: AppBar(
          title: Text('Error'),
        ),
        body: Center(
          child: Text('UnderConstruction'),
        ),
      );
    });
  }
}

我要导航到的页面

class FirstGiftPage extends StatefulWidget {
  String occasionID;
  String occasionTitle;
  static LatLng storeLocation;
  FirstGiftPage(this.occasionID,this.occasionTitle);
   @override
  _FirstGiftPageState createState() => _FirstGiftPageState();
}

class _FirstGiftPageState extends State<FirstGiftPage> {
  TextEditingController giftTitleTxtField = TextEditingController();
  TextEditingController giftLinkTxtField = TextEditingController();
  TextEditingController giftPriceTxtField = TextEditingController();
  TextEditingController giftStoreAddTxtField = TextEditingController();
  int giftCounter = 0;
  TextEditingController detailsTxtField = TextEditingController();
  Future<File> imageFile;


  nextTapped()async{
    if(giftTitleTxtField.text.isEmpty || giftLinkTxtField.text.isEmpty || giftPriceTxtField.text.isEmpty || detailsTxtField.text.isEmpty)
      ErrorOverlay("Invalid Data",false);
    else
      {
        Map<String,dynamic> giftData={
          "giftName" : giftTitleTxtField.text,
          "giftLink" : giftLinkTxtField.text,
          "giftPrice" : giftPriceTxtField.text,
          "storeAddress": giftStoreAddTxtField.text,
          "details" : detailsTxtField.text,
          "giftQuantity" : giftCounter,
          "images":[imageFile.toString()],
          "occasionID": widget.occasionID
        };
        ErrorOverlay("Gift Done",true);
        Navigator.of(context).pushReplacementNamed("/homepage");
      }
  }

  showMaps(){
    Navigator.of(context).pushNamed("/map");
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
          gradient: LinearGradient(
              colors: [
                Color.fromRGBO(246, 76, 77, 1),
                Color.fromRGBO(156, 51, 117, 1)
              ],
              begin: Alignment .topRight,
              end: Alignment.bottomLeft
          )
      ),
      child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar:AppBar(
          automaticallyImplyLeading: false,
          backgroundColor: Colors.transparent,
          elevation: 0,
          bottom: PreferredSize(
            preferredSize: Size(0.0, 80.0),
            child: Padding(
              //TODO::Change padding to ScreenWidth - 300 / 2
              padding: const EdgeInsets.only(left: 55.0 ,right: 55.0,bottom: 30.0),
              child: Text(AppLocalizations.of(context).firstGift,style: TextStyle(color: Colors.white,fontSize: 30.0),textAlign: TextAlign.center,maxLines: 2,),
            ),
          ),
        ),
        body: Padding(
          padding: const EdgeInsets.only(top: 20.0,left: 15.0,right: 15.0),
          child: Align(
            alignment: Alignment.topCenter,
            child: Column(
              children: [
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: TextFormField(
                    controller: giftTitleTxtField,
                    obscureText: false,
                    decoration: InputDecoration(
                        filled: true,
                        fillColor: Colors.white,
                        contentPadding:EdgeInsets.only(top:20.0,bottom:20.0,left: 15.0),
                        hintText: AppLocalizations.of(context).giftName,
                        border: OutlineInputBorder(
                          borderSide: BorderSide(
                              color: Colors.white,
                              width: 1
                          ),
                          borderRadius: BorderRadius.circular(30.0),
                        ),
                        enabledBorder: OutlineInputBorder(
                          borderSide: BorderSide(
                              color: Colors.white,
                              width: 1
                          ),
                          borderRadius: BorderRadius.circular(30.0),
                        ),
                        prefix: Container(width: 10.0,),
                        hintStyle: TextStyle(
                          fontWeight: FontWeight.w600,
                          fontSize: 16,
                        )
                    ),),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: TextFormField(
                    controller: giftLinkTxtField,
                    obscureText: false,
                    decoration: InputDecoration(
                        filled: true,
                        fillColor: Colors.white,
                        contentPadding:EdgeInsets.only(top:20.0,bottom:20.0,left: 15.0),
                        hintText: AppLocalizations.of(context).giftLink,
                        border: OutlineInputBorder(
                          borderSide: BorderSide(
                              color: Colors.white,
                              width: 1
                          ),
                          borderRadius: BorderRadius.circular(30.0),
                        ),
                        enabledBorder: OutlineInputBorder(
                          borderSide: BorderSide(
                              color: Colors.white,
                              width: 1
                          ),
                          borderRadius: BorderRadius.circular(30.0),
                        ),
                        prefix: Container(width: 10.0,),
                        hintStyle: TextStyle(
                          fontWeight: FontWeight.w600,
                          fontSize: 16,
                        )
                    ),),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: TextFormField(
                    controller: giftPriceTxtField,
                    obscureText: false,
                    decoration: InputDecoration(
                        filled: true,
                        fillColor: Colors.white,
                        contentPadding:EdgeInsets.only(top:20.0,bottom:20.0,left: 15.0),
                        hintText: AppLocalizations.of(context).giftPrice,
                        border: OutlineInputBorder(
                          borderSide: BorderSide(
                              color: Colors.white,
                              width: 1
                          ),
                          borderRadius: BorderRadius.circular(30.0),
                        ),
                        enabledBorder: OutlineInputBorder(
                          borderSide: BorderSide(
                              color: Colors.white,
                              width: 1
                          ),
                          borderRadius: BorderRadius.circular(30.0),
                        ),
                        prefix: Container(width: 10.0,),
                        hintStyle: TextStyle(
                          fontWeight: FontWeight.w600,
                          fontSize: 16,
                        )
                    ),),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: TextFormField(
                    readOnly: true,
                    onTap: showMaps,
                    controller: giftStoreAddTxtField,
                    obscureText: false,
                    decoration: InputDecoration(
                        filled: true,
                        fillColor: Colors.white,
                        contentPadding:EdgeInsets.only(top:20.0,bottom:20.0,left: 15.0),
                        hintText: AppLocalizations.of(context).giftStoreAdd,
                        border: OutlineInputBorder(
                          borderSide: BorderSide(
                              color: Colors.white,
                              width: 1
                          ),
                          borderRadius: BorderRadius.circular(30.0),
                        ),
                        enabledBorder: OutlineInputBorder(
                          borderSide: BorderSide(
                              color: Colors.white,
                              width: 1
                          ),
                          borderRadius: BorderRadius.circular(30.0),
                        ),
                        hintStyle: TextStyle(
                          fontWeight: FontWeight.w600,
                          fontSize: 16,
                        )
                    ),),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(AppLocalizations.of(context).giftQuantity,style: TextStyle(color: Colors.white),),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Counter(
                          initialValue: giftCounter,
                          minValue: 0,
                          maxValue: 10,
                          step: 1,
                          decimalPlaces: 0,
                          textStyle: TextStyle(color: Colors.white),
                          onChanged: (value) { // get the latest value from here
                            setState(() {
                              giftCounter = value;
                            });
                          },
                        ),
                      ),
                    ],
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: TextFormField(
                    enabled: false,
                    obscureText: false,
                    decoration: InputDecoration(
                        filled: true,
                        fillColor: Colors.white,
                        contentPadding: EdgeInsets.only(top: 20),
                        hintText: widget.occasionTitle,
                        border: OutlineInputBorder(
                          borderSide: BorderSide(
                              color: Colors.white,
                              width: 1
                          ),
                          borderRadius: BorderRadius.circular(20.0),
                        ),
                        enabledBorder: OutlineInputBorder(
                          borderSide: BorderSide(
                              color: Colors.white,
                              width: 1
                          ),
                          borderRadius: BorderRadius.circular(20.0),
                        ),
                        prefix: Container(width: 10.0,),
                        hintStyle: TextStyle(
                          fontWeight: FontWeight.w600,
                          fontSize: 16,
                        )
                    ),),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: TextFormField(
                    maxLines: null,
                    textAlign: TextAlign.center,
                    textAlignVertical: TextAlignVertical.center,
                    controller: detailsTxtField,
                    obscureText: false,
                    decoration: InputDecoration(
                        filled: true,
                        fillColor: Colors.white,
                        contentPadding: EdgeInsets.only(top: 20),
                        hintText: AppLocalizations.of(context).details,
                        border: OutlineInputBorder(
                          borderSide: BorderSide(
                              color: Colors.white,
                              width: 1
                          ),
                          borderRadius: BorderRadius.circular(20.0),
                        ),
                        enabledBorder: OutlineInputBorder(
                          borderSide: BorderSide(
                              color: Colors.white,
                              width: 1
                          ),
                          borderRadius: BorderRadius.circular(20.0),
                        ),
                        prefix: Container(width: 10.0,),
                        hintStyle: TextStyle(
                            fontWeight: FontWeight.w600,
                            fontSize: 16,
                            height: 4.0
                        )
                    ),),
                ),
                InkWell(
                  onTap: null,
                  child: Align(
                    alignment: Alignment.centerRight,
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Container(
                        width: 50.0,
                        height: 50.0,
                        padding: const EdgeInsets.all(8.0),
                        decoration: BoxDecoration(
                          border: Border.all(color: Colors.white,width: 4.0),
                          borderRadius: BorderRadius.circular(2.0)
                        ),
                        child: Center(
                          child: Icon(Icons.camera_alt_outlined,color: Colors.white,size: 25.0,),
                        ),
                      ),
                    ),
                  ),
                )
              ],
            ),
          ),
        ),
        bottomNavigationBar: BottomAppBar(
          child: Container(
            child: Wrap(
              //mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Align(
                    alignment: MyApp.getLocale(context).languageCode == "ar" ? Alignment.centerLeft:Alignment.centerRight,
                    child: Text("(4/5)"),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(8.0),
                    child: LinearProgressIndicator(
                      minHeight: 10.0,
                      backgroundColor: Colors.white,
                      value: 0.80,
                      valueColor: AlwaysStoppedAnimation<Color>(Colors.orange),
                    ),
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: InkWell(
                          onTap: (){
                            Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (BuildContext context)=> FirstGiftPage("","")));
                          },
                          child: Text(AppLocalizations.of(context).ignore,style: TextStyle(decoration: TextDecoration.underline),)),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: InkWell(
                        onTap: nextTapped,
                        child: Container(
                          width: 80.0,
                          padding: EdgeInsets.all(8.0),
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(10.0),
                              gradient: LinearGradient(
                                  colors: [
                                    Color.fromRGBO(246, 76, 77, 1),
                                    Color.fromRGBO(156, 51, 117, 1)
                                  ],
                                  begin: Alignment .topRight,
                                  end: Alignment.bottomLeft
                              )
                          ),
                          child: Center(child: Text(AppLocalizations.of(context).next, style: TextStyle(color: Colors.white),)),
                        ),
                      ),
                    )
                  ],
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}

这是错误

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

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

这是其中一位违规英雄的子树:Hero 标签: 状态:_HeroState#48fb7 抛出异常时,这是堆栈: #0 英雄._allHeroesFor.inviteHero。 >(包:flutter/src/widgets/heroes.dart:268:11) #1 Hero._allHeroesFor.inviteHero (package:flutter/src/widgets/heroes.dart:279:8) #2 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:298:21) #3 SingleChildRenderObjectElement.visitChildren >(package:flutter/src/widgets/framework.dart:6105:14) #4 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:311:15) ... ══════════════════════════════════════════════════ ══════════════════════════════════════════════════

【问题讨论】:

  • 您是否使用浮动操作按钮>
  • 不,甚至没有,甚至没有任何英雄动画

标签: flutter flutter-layout


【解决方案1】:

如果您使用多个floating action button,那么您需要像这样将自己的标签传递给每个按钮

FloatingActionButton(
    heroTag: "tag1",
)

FloatingActionButton(
    heroTag: "tag2",
)

默认情况下,heroTagtag,所以在这种情况下,您会收到错误消息。

第二件事

如果您在同一页面上使用多个hero animation widget并共享相同的标签那么同样的错误存在所以你需要将您的自己的标签传递给每个英雄小部件

【讨论】:

  • 问题是我没有使用任何浮动操作按钮或英雄动画,我仍然收到此错误,从一个屏幕导航到另一个屏幕时会出现什么问题?
  • @FarahMakhlouf 请分享您要导航的页面的代码
  • 我刚刚用页面的代码编辑了问题
【解决方案2】:

我通过为要导航到的页面创建一个新文件解决了这个问题,它工作正常,不知道如何

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-14
    • 2022-11-16
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 2022-11-17
    • 2012-10-21
    • 1970-01-01
    相关资源
    最近更新 更多