问题
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
回答1:
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),
),
);
}
}
来源:https://stackoverflow.com/questions/64389212/the-relevant-error-causing-widget-was-scaffold