android - 相关的导致错误的小部件是 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
类,children
小部件实例用Containers
. 检查评论以指导您。
可能使用应用程序 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),
),
);
}
}
推荐阅读
- android - Android Studio 应用程序开发中 Anychart 代码中的“私人访问”错误
- java - Integer类如何在不调用字段或方法的情况下直接存储值
- c# - EF Core 自引用一对多,在删除时设置 null 并在更新时级联可能会导致循环或多个路径
- python - AUTH_USER_MODEL 是指尚未安装的模型'auth_app.AuthAppShopUser'
- azure - 我可以将 Microsoft Azure WAF 添加为独立资源,而不是使用应用程序网关或前门吗?
- r - 在R中使用magick将图像矢量写入* .gif缓慢或失败?
- xamarin.forms - 错误:
预期包 com.companyname.final; 在 R.java 中 - javascript - TypeError:无法读取未定义的属性“getItem”(第 115 行,文件“代码”)
- powerbi - 间歇性错误:Prem 服务器上带有 SSAS 的 DAX 表达式
- python - Celery 任务未运行并卡在 PENDING 中