The relevant error-causing widget was Scaffold
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),
),
);
}
}
achinthaishane
Skilled in mobile development with iOS, Flutter and Firebase. Strong IT skills professional with a Bachelor degree focused in Computer Software Engineering from the University of Plymouth.
Updated on December 25, 2022Comments
-
achinthaishane over 1 year
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?
-
Marcos Boaventura over 3 yearsprobably the error is in the way you are structuring the screens.
-