原代码如下:将TabBar放在Column中,tabs默认为空数组[ ],进入页面后请求数据,再通过setState()更新TabBar,setState改变tabs长度之后抛出异常
List _tabs = []; //此处_tabs泛型为widget
TabController _tabController;
@override
void initState() {
super.initState();
_tabController = TabController(length: 0, vsync: this);
_handleRefresh();
}
_handleRefresh() {
Dao.fetch().then((Model model) {
//更新_tabs
model.tabs.map((it) {
return _tabs.add(Tab(
text: it.labelName,
));
}).toList();
_tabController = TabController(length: _tabs.length, vsync: this);
setState(() {});
}).catchError((e) {
print(e.toString());
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisSize: MainAxisSize.min,
children: [
TabBar(
controller: _tabController,
tabs: _tabs,
isScrollable: true,
labelColor: Colors.black,
labelPadding: EdgeInsets.fromLTRB(20, 0, 10, 5),
indicator: UnderlineTabIndicator(
borderSide: BorderSide(color: Color(0xff2fcfbb), width: 3)),
),
],
),
);
}
抛出异常:
I/flutter (15392): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (15392): The following RangeError was thrown building TabBar(dirty, dependencies: [_InheritedTheme,
I/flutter (15392): _LocalizationsScope-[GlobalKey#982b5]], state: _TabBarState#17353):
I/flutter (15392): RangeError (index): Invalid value: Valid value range is empty: 0
I/flutter (15392):
I/flutter (15392): When the exception was thrown, this was the stack:
I/flutter (15392): #0 List.[] (dart:core/runtime/libgrowable_array.dart:145:60)
I/flutter (15392): #1 _TabBarState.build (package:flutter/src/material/tabs.dart:954:26)
I/flutter (15392): #2 StatefulElement.build (package:flutter/src/widgets/framework.dart:3825:27)
I/flutter (15392): #3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3736:15)
I/flutter (15392): #4 Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (15392): #5 StatefulElement.update (package:flutter/src/widgets/framework.dart:3894:5)
I/flutter (15392): #6 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (15392): #7 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:4601:32)
I/flutter (15392): #8 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4992:17)
I/flutter (15392): #9 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (15392): #10 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (15392): #11 Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (15392): #12 ProxyElement.update (package:flutter/src/widgets/framework.dart:4006:5)
I/flutter (15392): #13 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (15392): #14 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (15392): #15 Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (15392): #16 ProxyElement.update (package:flutter/src/widgets/framework.dart:4006:5)
I/flutter (15392): #17 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (15392): #18 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:4601:32)
I/flutter (15392): #19 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4992:17)
I/flutter (15392): #20 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (15392): #21 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (15392): #22 Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (15392): #23 StatefulElement.update (package:flutter/src/widgets/framework.dart:3894:5)
I/flutter (15392): #24 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (15392): #25 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (15392): #26 Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (15392): #27 ProxyElement.update (package:flutter/src/widgets/framework.dart:4006:5)
I/flutter (15392): #28 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (15392): #29 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (15392): #30 Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (15392): #31 StatefulElement.update (package:flutter/src/widgets/framework.dart:3894:5)
I/flutter (15392): #32 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (15392): #33 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4883:14)
I/flutter (15392): #34 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (15392): #35 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (15392): #36 Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (15392): #37 StatelessElement.update (package:flutter/src/widgets/framework.dart:3796:5)
I/flutter (15392): #38 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (15392): #39 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4883:14)
I/flutter (15392): #40 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (15392): #41 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (15392): #42 Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (15392): #43 StatefulElement.update (package:flutter/src/widgets/framework.dart:3894:5)
I/flutter (15392): #44 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (15392): #45 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (15392): #46 Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (15392): #47 StatefulElement.update (package:flutter/src/widgets/framework.dart:3894:5)
I/flutter (15392): #48 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (15392): #49 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (15392): #50 Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (15392): #51 ProxyElement.update (package:flutter/src/widgets/framework.dart:4006:5)
I/flutter (15392): #52 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (15392): #53 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (15392): #54 Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (15392): #55 ProxyElement.update (package:flutter/src/widgets/framework.dart:4006:5)
I/flutter (15392): #56 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (15392): #57 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (15392): #58 Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (15392): #59 StatefulElement.update (package:flutter/src/widgets/framework.dart:3894:5)
I/flutter (15392): #60 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (15392): #61 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (15392): #62 Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (15392): #63 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2273:33)
I/flutter (15392): #64 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:700:20)
I/flutter (15392): #65 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:268:5)
I/flutter (15392): #66 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:988:15)
I/flutter (15392): #67 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:928:9)
I/flutter (15392): #68 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:840:5)
I/flutter (15392): #72 _invoke (dart:ui/hooks.dart:209:10)
I/flutter (15392): #73 _drawFrame (dart:ui/hooks.dart:168:3)
I/flutter (15392): (elided 3 frames from package dart:async)
I/flutter (15392): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter (15392): Another exception was thrown: A RenderFlex overflowed by 99458 pixels on the bottom.
解决方法:如下所示,将tabs的生成放到TabBar中
List _tabs = []; //此处_tabs泛型为TabItem(自定义的Model)
TabController _tabController;
@override
void initState() {
super.initState();
_tabController = TabController(length: 0, vsync: this);
_handleRefresh();
}
_handleRefresh() {
TravelTabDao.fetch().then((TabModel model) {
_tabs = model.tabs;
_tabController = TabController(length: _tabs.length, vsync: this);
setState(() {});
}).catchError((e) {
print(e.toString());
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisSize: MainAxisSize.min,
children: [
TabBar(
controller: _tabController,
//由_tabs生成TabBar的tab
tabs: _tabs.map((TabItem tab) {
return Tab(
text: tab.labelName,
);
}).toList(),
isScrollable: true,
labelColor: Colors.black,
labelPadding: EdgeInsets.fromLTRB(20, 0, 10, 5),
indicator: UnderlineTabIndicator(
borderSide: BorderSide(color: Color(0xff2fcfbb), width: 3)),
),
],
),
);
}
具体原因我也不清楚,希望知道的大佬们能够指点一下