我正在将底部标签栏添加到我的flutter应用程序中。但是添加代码后,它会抛出一个错误。 这是我的代码:
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: new Text("Tab App."),
),
drawer: sideDrawer(),
bottomNavigationBar: bottomTabBar()
);
}
//Bottom Tab Bar
Widget bottomTabBar(){
return new TabBarView(
children:[
new Container(
color: Colors.yellow,
),
new Container(
color: Colors.orange,
),
new Container(
color: Colors.lightGreen,
),
new Container(
color: Colors.red,
)
],
);
}
这是错误:
flutter: When the exception was thrown, this was the stack:
flutter: #0 Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)
flutter: #1 _HomeState.drawerHeader (package:tabApp/screens/home.dart:72:40)
flutter: #2 _HomeState.sideDrawer (package: tabApp/screens/home.dart:91:13)
flutter: #3 _HomeState.build (package:tabApp/screens/home.dart:37:15)
flutter: #4 StatefulElement.build (package:flutter/src/widgets/framework.dart:3809:27)
flutter: #5 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3721:15)
flutter: #6 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
flutter: #7 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
flutter: #8 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
flutter: #9 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
flutter: #10 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #11 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #12 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
flutter: #13 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #14 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #15 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
flutter: #16 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
flutter: #17 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
flutter: #18 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
flutter: #19 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #20 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #21 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
flutter: #22 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #23 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #24 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
flutter: #25 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #26 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4965:32)
flutter: #27 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #28 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #29 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
flutter: #30 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
flutter: #31 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
flutter: #32 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
flutter: #33 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
flutter: #34 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #35 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #36 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
flutter: #37 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #38 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #39 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
flutter: #40 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
flutter: #41 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
flutter: #42 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
flutter: #43 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
flutter: #44 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #45 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #46 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
flutter: #47 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #48 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #49 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
flutter: #50 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
flutter: #51 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
flutter: #52 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
flutter: #53 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
flutter: #54 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #55 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #56 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
flutter: #57 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #58 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #59 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
flutter: #60 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
flutter: #61 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
flutter: #62 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
flutter: #63 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
flutter: #64 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #65 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #66 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
flutter: #67 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
flutter: #68 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
flutter: #69 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
flutter: #70 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #71 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #72 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
flutter: #73 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
flutter: #74 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
flutter: #75 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
flutter: #76 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
flutter: #77 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #78 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #79 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
flutter: #80 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #81 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #82 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
flutter: #83 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
flutter: #84 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
flutter: #85 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
flutter: #86 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #87 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #88 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
flutter: #89 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #90 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #91 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
flutter: #92 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
flutter: #93 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
flutter: #94 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
flutter: #95 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
flutter: #96 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #97 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #98 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
flutter: #99 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
flutter: #100 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
flutter: #101 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
flutter: #102 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #103 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #104 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
flutter: #105 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #106 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #107 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
flutter: #108 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
flutter: #109 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
flutter: #110 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
flutter: #111 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #112 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #113 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
flutter: #114 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
flutter: #115 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
flutter: #116 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
flutter: #117 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
flutter: #118 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #119 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #120 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
flutter: #121 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
flutter: #122 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
flutter: #123 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
flutter: #124 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
flutter: #125 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
flutter: #126 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
flutter: #127 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:4643:32)
flutter: #128 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4975:17)
flutter: #129 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
flutter: #130 _TheatreElement.update (package:flutter/src/widgets/overlay.dart:507:16)
flutter: #131 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
flutter: #132 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
flutter: #133 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
flutter: #134 StatefulElement.update (package:flutter/src/widgets/framework.dart:3878:5)
flutter: #135 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
flutter: #136 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
flutter: #137 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
flutter: #138 ProxyElement.update (package:flutter/src/widgets/framework.dart:3990:5)
flutter: #139 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
flutter: #140 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4867:14)
flutter: #141 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
flutter: #142 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
flutter: #143 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
flutter: #144 StatefulElement.update (package:flutter/src/widgets/framework.dart:3878:5)
flutter: #145 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
flutter: #146 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4867:14)
flutter: #147 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
flutter: #148 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4867:14)
flutter: #149 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
flutter: #150 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
flutter: #151 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
flutter: #152 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2286:33)
flutter: #153 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:676:20)
flutter: #154 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:219:5)
flutter: #155 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:990:15)
flutter: #156 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:930:9)
flutter: #157 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:842:5)
flutter: #158 _invoke (dart:ui/hooks.dart:154:13)
flutter: #159 _drawFrame (dart:ui/hooks.dart:143:3)
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════
flutter: Another exception was thrown: No TabController for TabBarView.
答案 0 :(得分:0)
由于错误提示您需要-TabController
如下编辑代码。
@override
Widget build(BuildContext context) {
// TODO: implement build
return DefaultTabController( // Added
length: 4, // Added
initialIndex: 0, //Added
child: Scaffold(
appBar: AppBar(
title: new Text("Tab App."),
),
drawer: sideDrawer(context), // Passed BuildContext in function.
bottomNavigationBar: bottomTabBar()),
);
}
Widget sideDrawer(BuildContext context){ ......