Widget

FloatingActionButton 여러개 사용시 에러및 오류 해결 하는 법.

까사파파 2024. 2. 3. 17:15

 

 

플로팅 버튼이 쉽고 잘만들어진 버튼이지만, 써보면 에러가 나는 경우가

자주 있습니다. 이유는 이쁘다고 버튼을 여러개 쓰는 경우가 있고,

실행은 되는데 에러메시지가 나온다. 찾아보면 답이 있습니다.

======== Exception caught by scheduler library =====================================================
The following assertion was thrown during a scheduler callback:
There are multiple heroes that share the same tag within a subtree.

Within each subtree for which heroes are to be animated (i.e. a PageRoute subtree), each Hero must have a unique non-null tag.
In this case, multiple heroes had the following tag: <default FloatingActionButton tag>
Here is the subtree for one of the offending heroes: Hero
  tag: <default FloatingActionButton tag>
  state: _HeroState#196c3
When the exception was thrown, this was the stack: 
#0      Hero._allHeroesFor.inviteHero.<anonymous closure> (package:flutter/src/widgets/heroes.dart:277:11)
#1      Hero._allHeroesFor.inviteHero (package:flutter/src/widgets/heroes.dart:288:8)
#2      Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:306:11)
#3      SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6722:14)
#4      Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:321:15)
#5      ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5523:14)
 

 각 히로는 유이크 아이템인 Tag 를 가져야 한다고 합니다.

each Hero must have a unique non-null tag.

 

아래 오류 코드에 . FloatingActionButton 내부에

태그를 작성해 주면 됩니다.

heroTag:

          FloatingActionButton(
              onPressed: () {
                Navigator.push(context, MaterialPageRoute(builder: (context) => AddTodo()));
              },
              tooltip: 'Add Todo',
              child: const Icon(Icons.add)),
          FloatingActionButton(
              onPressed: () {
                //context.read<TodoCubit>()
                todoCubit.subTodo();
              },
              tooltip: 'Subtract Todo',
              child: const Icon(Icons.minimize)),
 

정정된 코드   heroTag: 'addTodo', ....

 FloatingActionButton(
              heroTag: 'addTodo',
              onPressed: () {
                Navigator.push(context, MaterialPageRoute(builder: (context) => AddTodo()));
              },
              tooltip: 'Add Todo',
              child: const Icon(Icons.add)),
          FloatingActionButton(
              heroTag: 'SubTodo',
              onPressed: () {
                //context.read<TodoCubit>()
                todoCubit.subTodo();
              },
              tooltip: 'Subtract Todo',
              child: const Icon(Icons.minimize)),