【发布时间】:2021-01-30 12:01:09
【问题描述】:
我在 Flutter 中创建了一个包含 bottomnavigationbar 的简单代码,但它不起作用。我的代码如下:
class MainPage extends StatefulWidget{
@override
DashboardScreen createState() {
return DashboardScreen();
}
}
class DashboardScreen extends State<MainPage> {
Widget callPage(int currentIndex) {
switch (currentIndex) {
case 0 : return MainPage();
case 1 : return SecondPage();
case 2 : return ThirdPage();
break;
default: MainPage();
}
}
int _currentIndex = 0;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Dashboard',
home: Scaffold(
appBar: AppBar(
title: Text('Dashboard'),
),
body: callPage(_currentIndex),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
type: BottomNavigationBarType.fixed,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
backgroundColor: Colors.blue
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
label: 'Settings',
backgroundColor: Colors.blue
),
BottomNavigationBarItem(
icon: Icon(Icons.account_circle),
label: 'Profile',
backgroundColor: Colors.blue
)
],
onTap: (index) {
setState((){
_currentIndex = index;
print(_currentIndex);
print("setstate");
});
},
),
)
);
}
但是现在,当我将它部署到 android 模拟器时,我收到以下错误:
════════ 小部件库捕获的异常 ══════════════════════════════════════════════════ ═════堆栈溢出 相关的导致错误的小部件是:MaterialApp file:///Users/user/AndroidStudioProjects/myapp/lib/dashboard.dart:34:12 ══════════════════════════════════════════════════ ══════════════════════════════════════════════════
════════ 小部件库捕获的异常 ══════════════════════════════════════════════════ ═════ 'package:flutter/src/widgets/framework.dart':断言失败:行 4345 pos 14:'owner._debugCurrentBuildTarget == this':不正确。 相关的导致错误的小部件是:MaterialApp file:///Users/user/AndroidStudioProjects/myapp/lib/dashboard.dart:34:12
第 34 行是这样的:
return MaterialApp(
此错误一直附加在控制台中,直到我与设备断开连接。为什么会这样,我怎样才能让我的bottomnavigationbar 工作?谢谢!
【问题讨论】:
-
对我来说
BottomNavigationBarItem没有label。你的颤振版本是什么? -
@Marcos 我收到错误,脚手架错误请检查stackoverflow.com/questions/64389212/…
标签: flutter