我试图在Flutter中使用BottomNavigationBar
,但是这给了我一个奇怪的错误,该错误似乎没有引用我的代码,并且我无法在任何在线位置找到此错误。实际上,我可以找到的每个教程,页面或问题都说要这样做。
这是我来自home.dart
的代码:
... Imports ...
class HomePage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return HomePageState();
}
}
class HomePageState extends State<HomePage> {
int _selectedTab = 0;
final _pageOptions = [
HomePage(),
CatPage(),
SearchPage(),
];
void _onItemTapped(int index) {
setState(() {
_selectedTab = index;
});
print(index);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Loopt In'),
),
body: _pageOptions[_selectedTab],
bottomNavigationBar: BottomNavigationBar(
currentIndex: _selectedTab,
onTap: _onItemTapped,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(Icons.category),
title: Text('Categories'),
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
title: Text('Search'),
),
],
),
);
}
}
当我将_pageOptions
数组更改为具有Text()
小部件而不是使它正常工作的小部件时,足够有趣,但是我不知道为什么,显然需要它与我的小部件一起使用
这是我遇到的错误:
[VERBOSE-2:shell.cc(181)] Dart Error: Unhandled exception:
'package:flutter/src/widgets/framework.dart': Failed assertion: line 2257 pos 20: '_debugCurrentBuildTarget == context': is not true.
#0 _AssertionError._doThrowNew (dart:core/runtime/liberrors_patch.dart:40:39)
#1 _AssertionError._throwNew (dart:core/runtime/liberrors_patch.dart:36:5)
#2 BuildOwner.buildScope.<anonymous closure> (package:flutter/src/widgets/framework.dart:2257:20)
#3 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2261:12)
#4 RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:817:13)
#5 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:709:7)
#6 runApp (package:flutter/src/widgets/binding.dart:748:7)
#7 main (file:///Users/garrettlove/Documents/learn/Flutter/loopt_in/lib/main.d<…>
答案 0 :(得分:0)
导致问题的原因是 HomePage()
设置为正文,从而导致错误循环。
final _pageOptions = [
HomePage(), // replace this with a widget for the HomePage
CatPage(),
SearchPage(),
];
要解决此问题,请将 HomePage()
替换为小部件以填充主页屏幕。