[英]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':断言失败:第 4345 行 pos 14:'owner._debugCurrentBuildTarget == this':不是真的。 相关的导致错误的小部件是 Scaffold
为什么我收到这个错误?
我假设错误来自HomeScreen
小部件。 由于错误消息显示lib/.../home/home.dart
。
我确实发现当前代码段没有任何问题,实际上您可以检查此处运行的代码。 本答案末尾提供了源代码。
我只重命名了_NavBarState
类,并且使用Containers
_NavBarState
了children
小部件实例。 检查评论以指导您。
可能使用应用程序 MainScreen 和 HomeScreen 类的完整代码可以提供更准确的答案。
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),
),
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.