Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The relevant error-causing widget was Scaffold

class _NavBarState extends State<NavBar> {
  int _currentIndex = 0;
  final List<Widget> _children = [
    HomeScreen(),
    SignUpScreen(),
    ForgetPassword(),
    LoginScreen(),
  ];

  void onTappedBar(int index) {
    setState(() {
      _currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _children[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        onTap: onTappedBar,
        currentIndex: _currentIndex,
        type: BottomNavigationBarType.fixed,
        items: [
          BottomNavigationBarItem(
              icon: Icon(
                Icons.search,
              ),
              label: 'Search',
              backgroundColor: Colors.black),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.favorite,
              ),
              label: 'Favorites',
              backgroundColor: Colors.black),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.notifications,
              ),
              label: 'Notifications',
              backgroundColor: Colors.black),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.more,
              ),
              label: 'More',
              backgroundColor: Colors.black),
        ],
        selectedItemColor: Color(0xffFFDA3A),
      ),
    );
  }
}

'package:flutter/src/widgets/framework.dart': Failed assertion: line 4345 pos 14: 'owner._debugCurrentBuildTarget == this': is not true. The relevant error-causing widget was Scaffold

Why am I getting this error?

link to the error

like image 584
achinthaishane Avatar asked May 25 '26 15:05

achinthaishane


1 Answers

I am supposing the error is coming from HomeScreen widget. As the error message shows lib/.../home/home.dart.

I did find nothing wrong with the current snippet and in fact you can check the code running here. Source code is provided at the end of this answer.

I only renamed the _NavBarState class and the children widget instances are mocked with Containers. Check the comments to guide you.

Probably with the full code of the app MainScreen and HomeScreen classes a more accurate answer could be provided.

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

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

// the old _NavBar class just renamed.
class AppMainPage extends StatefulWidget{
  @override
  _AppMainPageState createState() => _AppMainPageState();
}

// The old _NavBarState class, just renamed.
class _AppMainPageState extends State<AppMainPage> {
  int _currentIndex = 0;
  final List<Widget> _children = [
    //HomeScreen(), mocking widget 
    Container(
      color: Colors.red,
      child: Center(
        child: Text('Search body layout'),
      ),
    ),
    
    //SignUpScreen(), mocking widget
    Container(
      color: Colors.green,
      child: Center(
        child: Text('Favorites body layout'),
      ),
      
    ),
    
    //ForgetPassword(),mocking widget
    Container(
      color: Colors.blue,
      child: Center(
        child: Text('Notification body layout'),
      ),
    ),
    
     //LoginScreen(),mocking widget
    Container(
      color: Colors.amber,
      child: Center(
        child: Text('More body layout'),
      ),
    ),
    
  ];

  void onTappedBar(int index) {
    setState(() {
      _currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _children[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        onTap: onTappedBar,
        currentIndex: _currentIndex,
        type: BottomNavigationBarType.fixed,
        items: [
          BottomNavigationBarItem(
              icon: Icon(
                Icons.search,
              ),
              label: 'Search',
              backgroundColor: Colors.black),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.favorite,
              ),
              label: 'Favorites',
              backgroundColor: Colors.black),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.notifications,
              ),
              label: 'Notifications',
              backgroundColor: Colors.black),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.more,
              ),
              label: 'More',
              backgroundColor: Colors.black),
        ],
        selectedItemColor: Color(0xffFFDA3A),
      ),
    );
  }
}
like image 101
Marcos Boaventura Avatar answered May 27 '26 05:05

Marcos Boaventura



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!