Home > Mobile >  The relevant error-causing widget was Scaffold when the exception was thrown, this was the stack
The relevant error-causing widget was Scaffold when the exception was thrown, this was the stack

Time:10-17

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

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  final user = FirebaseAuth.instance.currentUser!;

  int index = 0;

  final pages = [
    Home(),
    Add(),
    Setting(),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: pages[index],
      bottomNavigationBar: NavigationBarTheme(
        data: NavigationBarThemeData(
            indicatorColor: Colors.purpleAccent.withOpacity(0.5),
            labelTextStyle: MaterialStateProperty.all(const TextStyle(
              fontSize: 14,
              fontWeight: FontWeight.bold,
            ))),
        child: NavigationBar(
          backgroundColor: Colors.white,
          animationDuration: const Duration(seconds: 1),
          labelBehavior: NavigationDestinationLabelBehavior.onlyShowSelected,
          height: 70,
          selectedIndex: index,
          onDestinationSelected: (index) {
            setState(() {
              this.index = index;
            });
          },
          destinations: const [
            NavigationDestination(
              selectedIcon: Icon(Icons.home),
              icon: Icon(Icons.home_outlined),
              label: '主頁',
            ),
            NavigationDestination(
              selectedIcon: Icon(Icons.add),
              icon: Icon(Icons.add_outlined),
              label: '加入',
            ),
            NavigationDestination(
              selectedIcon: Icon(Icons.settings),
              icon: Icon(Icons.settings_outlined),
              label: '設定',
            ),
          ],
        ),
      ),
    );
  }
}

The following StackOverflowError was thrown building _BodyBuilder: Stack Overflow

The relevant error-causing widget was Scaffold lib/Pages/home.dart:26 When the exception was thrown, this was the stack

As I am new in Flutter, can anyone tell me why am I getting this error?

Thanks.

Link to the error

CodePudding user response:

The main issue is you are calling Home inside the pages, similar like recursive thing,

  final pages = [
    Text(""), // use different widget instead of Home
    Add(),
    Setting(),
  ];

  • Related