skip to Main Content

I am working on an app that have a home page and a settings page so i want to move between them with an icon button in the appbar so i used navigator.push method but the app freezez and dosenot do any thing and when i restart it gives me this error :

══╡ 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

When the exception was thrown, this was the stack:
#0      Hero._allHeroesFor.inviteHero.<anonymous closure> (package:flutter/src/widgets/heroes.dart:278:11)
#1      Hero._allHeroesFor.inviteHero (package:flutter/src/widgets/heroes.dart:289:8)
#2      Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:307:11)
#3      SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6420:14)
#4      Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#5      ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5349:14)
#6      Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#7      MultiChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6532:16)
#8      Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#9      SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6420:14)
#10     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#11     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5349:14)
#12     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#13     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6420:14)
#14     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#15     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5349:14)
#16     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#17     MultiChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6532:16)
#18     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#19     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5349:14)
#20     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#21     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5349:14)
#22     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#23     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5349:14)
#24     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#25     MultiChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6532:16)
#26     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#27     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5349:14)
#28     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#29     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5349:14)
#30     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#31     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5349:14)
#32     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#33     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5349:14)
#34     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#35     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5349:14)
#36     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#37     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6420:14)
#38     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#39     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5349:14)
#40     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#41     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6420:14)
#42     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#43     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5349:14)
#44     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#45     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5349:14)
#46     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#47     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5349:14)
#48     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#49     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5349:14)
#50     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#51     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5349:14)
#52     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#53     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5349:14)
#54     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#55     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5349:14)
#56     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#57     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5349:14)
#58     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#59     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5349:14)
#60     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#61     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6420:14)
#62     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#63     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5349:14)
#64     Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:322:15)
#65     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6420:14)
#66     Element.visitChildElements (package:flutter/src/widgets/framework.dart:3581:5)
#67     Hero._allHeroesFor (package:flutter/src/widgets/heroes.dart:325:13)
#68     HeroController._startHeroTransition (package:flutter/src/widgets/heroes.dart:932:14)
#69     HeroController._maybeStartHeroTransition.<anonymous closure> (package:flutter/src/widgets/heroes.dart:891:9)
#70     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1297:15)
#71     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1236:9)
#72     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1085:5)
#73     _invoke (dart:ui/hooks.dart:170:13)
#74     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:401:5)
#75     _drawFrame (dart:ui/hooks.dart:140:31)
════════════════════════════════════════════════════════════════════════════════════════════════════

Restarted application in 1,972ms.

i have two files of code :

main.dart:

import 'package:flutter/material.dart';
import 'package:flutter_tts/flutter_tts.dart';
import 'settings.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        useMaterial3: false,
      ),
      debugShowCheckedModeBanner: false,
      home: MainSt(),
    );
  }
}

class MainSt extends StatefulWidget {
  const MainSt({Key? key}) : super(key: key);

  @override
  State<MainSt> createState() => _MainStState();
}

final addController = TextEditingController();
List cardsList = [];

class _MainStState extends State<MainSt> {
  var tts_delay = 1;
  var tts_word_repeat = 2;
  var tts_speach_rate = 0.5;

  FlutterTts flutterTts = FlutterTts();
  Future<void> textToS(String text) async {
    await flutterTts.setLanguage("en-US");
    await flutterTts.setVolume(0.5);
    await flutterTts.setSpeechRate(tts_speach_rate);
    await flutterTts.setPitch(1);
    await flutterTts.speak(text);
  }

  bool isPlaying = false;

  Future<void> playAllPhrases() async {
    isPlaying = true; // Set the flag to indicate playback is ongoing

    List<String> tempCardsList =
        List.from(cardsList); // Create a temporary list

    for (String phrase in tempCardsList) {
      if (!isPlaying) break; // Check the flag and break if needed
      for (int word = 0; word < tts_word_repeat; word++) {
        if (!isPlaying) break; // Check the flag and break if needed
        await textToS(phrase);
        await Future.delayed(Duration(seconds: tts_delay));
      }
    }
  }

  Future<void> _dialogBuilder(BuildContext context) {
    return showDialog<void>(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('Add a word or phrase'),
          content: TextField(
            controller: addController,
            decoration: const InputDecoration(
              border: UnderlineInputBorder(),
              labelText: 'Enter word or phrase',
            ),
          ),
          actions: <Widget>[
            TextButton(
              style: TextButton.styleFrom(
                textStyle: Theme.of(context).textTheme.labelLarge,
              ),
              child: const Text('Cancel'),
              onPressed: () {
                Navigator.of(context).pop();
                addController.clear();
              },
            ),
            TextButton(
              style: TextButton.styleFrom(
                textStyle: Theme.of(context).textTheme.labelLarge,
              ),
              child: const Text('Done'),
              onPressed: () {
                print(addController.text);
                setState(
                  () {
                    if (!(addController.text.trim().isEmpty)) {
                      cardsList.add(addController.text);
                      addController.clear();
                      Navigator.of(context).pop();
                    } else {
                      addController.clear();
                    }
                  },
                );
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Text('Wordly'),
            IconButton(
              icon: Icon(Icons.settings),
              onPressed: () {
                setState(() {
                  Navigator.of(context).push(
                      MaterialPageRoute(builder: (context) => Settings_page()));
                });
              },
            ),
          ],
        ),
      ),
      floatingActionButton: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          FloatingActionButton(
            child: Icon(Icons.add),
            onPressed: () {
              _dialogBuilder(context);
            },
          ),
          SizedBox(
            height: 10,
          ),
          FloatingActionButton(
            child: Icon(Icons.volume_up_outlined),
            onPressed: () {
              playAllPhrases();
            },
          ),
          SizedBox(
            height: 10,
          ),
          FloatingActionButton(
            child: Icon(Icons.stop_circle),
            onPressed: () {
              setState(() {
                isPlaying = false;
              });
            },
          ),
        ],
      ),
      body: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: cardsList.length,
      itemBuilder: (context, index) {
        return Padding(
          padding: const EdgeInsets.symmetric(vertical: 2, horizontal: 5),
          child: Card(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                ListTile(
                  trailing: IconButton(
                    icon: Icon(Icons.delete),
                    onPressed: () {
                      setState(() {
                        //add card delete logic
                        cardsList.removeAt(index);
                      });
                    },
                  ),
                  title: Row(
                    children: [
                      // SizedBox(
                      //   width: 8,
                      // ),
                      Text(cardsList[index]),
                    ],
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}

and settings.dart :

import 'package:flutter/material.dart';
import 'main.dart';

class Settings_page extends StatefulWidget {
  const Settings_page({super.key});

  @override
  State<Settings_page> createState() => _Settings_pageState();
}

class _Settings_pageState extends State<Settings_page> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text("Hello"),
      ),
    );
  }
}

i tride the navigator method :

Navigator.of(context).push(MaterialPageRoute(builder: (context) => Settings_page()));

and i tride the navigator method without setstate but all lead to the same problem .

here is a video of the app :
Google Drive

i hope you finde a fix for it (:

2

Answers


  1. Pass value to key property in floatingactionbutton

    FloatingActionButton(key:ValueKey('1'),//you need to assign value to Every FloatingActionButton and use unique value in ValueKey for all buttons if you have multiple buttons 
            child: Icon(Icons.add),
            onPressed: () {
              _dialogBuilder(context);
            },
          ),
    FloatingActionButton(key:ValueKey('2'),
            child: Icon(Icons.add),
            onPressed: () {
              _dialogBuilder(context);
            },
          ),
    
    Login or Signup to reply.
  2. FAB (Floating Action Button) should not be used in that way, usually it refers to the ‘most important action’ or the ‘common action’ that user can do in that screen.

    if you did and had used more than one FAB in the screen (noticed that you are nesting 3 FABs within a column) so, you should provide different unique hero tag for every FAB.

    for more details, check that: https://blog.logrocket.com/flutter-floatingactionbutton-a-complete-tutorial-with-examples/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search