使用dio动态添加TabController报错

龙上 发布于 2020/04/18 14:48
阅读 339
收藏 0

开源之夏第三届火热来袭,高校学生参与赢万元奖金!>>>

initState里面那个getCategory是使用dio请求异步加载tab的标题信息,一直报错

import 'package:flutter/material.dart';

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> with TickerProviderStateMixin {
//  AnimationController _controller;
  TabController _tabController;
  List<Tab> titleTabs = <Tab>[];
  List<Widget> _listPage = List<Widget>();
  String _str = "data";

  @override
  void initState() {
    print("reg init");
//    _controller = AnimationController(vsync: this);
    super.initState();
    print("getCategory");
    _tabController = TabController(length: titleTabs.length, vsync: this)
      ..addListener((){
      print("callBack");
      setState(() {
        _str = "index";
      });
      switch (_tabController.index) {
        case 0:
          print(1);
          break;
        case 1:
          print(2);
          break;
        case 2:
          print(3);
          break;
      }
    });
    getCategory().then((v){
      v.records.forEach((l){
        titleTabs.add(Tab(
          text: "l.title",
        ));
        titleTabs.add(Tab(
          text: "l.title2",
        ));
        titleTabs.add(Tab(
          text: "l.title3",
        ));
      });
      getWidgets();
      setState(() {
        print("titleTabs");
        print(titleTabs.length);
        _tabController = new TabController(length: titleTabs.length, vsync: this);
      });
    });
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  List<Widget> getWidgets() {
    _listPage = List<Widget>();
    for (int i = 0; i < titleTabs.length; i++) {
      _listPage.add(
        Text(i.toString())
      );
    }
    return _listPage;
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: TabBar(
            controller: _tabController,
            isScrollable: true,
//            indicator: UnderlineTabIndicator( borderSide: BorderSide(style: BorderStyle.none)),
            tabs: titleTabs,
            onTap: (index){
              print("object:$index");

            },
        ),
      ),
      body: TabBarView(
        controller: _tabController,
        children: _listPage,
      ),
      bottomNavigationBar: BottomNavigationBar(
          items: [
            BottomNavigationBarItem(
              icon: Icon(Icons.video_call),
              title: Text("video"),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.menu),
              title: Text("my"),
            ),
          ]
      ),
      // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

报错信息:

Performing hot restart...
Syncing files to device Android SDK built for x86...
Restarted application in 2,023ms.
I/flutter ( 3944): reg init
I/flutter ( 3944): getCategory
I/flutter ( 3944): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 3944): The following RangeError was thrown building TabBar(dirty, dependencies: [_InheritedTheme,
I/flutter ( 3944): _LocalizationsScope-[GlobalKey#6edb0]], state: _TabBarState#2896d):
I/flutter ( 3944): RangeError (index): Invalid value: Valid value range is empty: 0
I/flutter ( 3944): 
I/flutter ( 3944): The relevant error-causing widget was:
I/flutter ( 3944):   TabBar file:///Volumes/data/app/video/lib/home/home.dart:74:16
I/flutter ( 3944): 
I/flutter ( 3944): When the exception was thrown, this was the stack:
I/flutter ( 3944): #0      List.[] (dart:core-patch/growable_array.dart:149:60)
I/flutter ( 3944): #1      _TabBarState.build (package:flutter/src/material/tabs.dart:1017:26)
I/flutter ( 3944): #2      StatefulElement.build (package:flutter/src/widgets/framework.dart:4334:27)
I/flutter ( 3944): #3      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4223:15)
I/flutter ( 3944): #4      Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #5      StatefulElement.update (package:flutter/src/widgets/framework.dart:4413:5)
I/flutter ( 3944): #6      Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #7      SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5452:14)
I/flutter ( 3944): #8      Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #9      SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5452:14)
I/flutter ( 3944): #10     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #11     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #12     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #13     ProxyElement.update (package:flutter/src/widgets/framework.dart:4557:5)
I/flutter ( 3944): #14     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #15     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #16     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #17     ProxyElement.update (package:flutter/src/widgets/framework.dart:4557:5)
I/flutter ( 3944): #18     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #19     RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5161:32)
I/flutter ( 3944): #20     MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5561:17)
I/flutter ( 3944): #21     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #22     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #23     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #24     StatelessElement.update (package:flutter/src/widgets/framework.dart:4298:5)
I/flutter ( 3944): #25     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #26     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #27     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #28     ProxyElement.update (package:flutter/src/widgets/framework.dart:4557:5)
I/flutter ( 3944): #29     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #30     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #31     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #32     ProxyElement.update (package:flutter/src/widgets/framework.dart:4557:5)
I/flutter ( 3944): #33     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #34     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #35     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #36     StatelessElement.update (package:flutter/src/widgets/framework.dart:4298:5)
I/flutter ( 3944): #37     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #38     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5452:14)
I/flutter ( 3944): #39     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #40     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5452:14)
I/flutter ( 3944): #41     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #42     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #43     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #44     ProxyElement.update (package:flutter/src/widgets/framework.dart:4557:5)
I/flutter ( 3944): #45     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #46     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5452:14)
I/flutter ( 3944): #47     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #48     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #49     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #50     StatelessElement.update (package:flutter/src/widgets/framework.dart:4298:5)
I/flutter ( 3944): #51     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #52     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5452:14)
I/flutter ( 3944): #53     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #54     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5452:14)
I/flutter ( 3944): #55     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #56     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #57     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #58     ProxyElement.update (package:flutter/src/widgets/framework.dart:4557:5)
I/flutter ( 3944): #59     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #60     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #61     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #62     StatefulElement.update (package:flutter/src/widgets/framework.dart:4413:5)
I/flutter ( 3944): #63     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #64     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5452:14)
I/flutter ( 3944): #65     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #66     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #67     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #68     StatelessElement.update (package:flutter/src/widgets/framework.dart:4298:5)
I/flutter ( 3944): #69     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #70     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5452:14)
I/flutter ( 3944): #71     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #72     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #73     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #74     StatefulElement.update (package:flutter/src/widgets/framework.dart:4413:5)
I/flutter ( 3944): #75     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #76     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #77     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #78     StatefulElement.update (package:flutter/src/widgets/framework.dart:4413:5)
I/flutter ( 3944): #79     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #80     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5452:14)
I/flutter ( 3944): #81     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #82     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5452:14)
I/flutter ( 3944): #83     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #84     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #85     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #86     StatefulElement.update (package:flutter/src/widgets/framework.dart:4413:5)
I/flutter ( 3944): #87     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #88     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #89     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #90     ProxyElement.update (package:flutter/src/widgets/framework.dart:4557:5)
I/flutter ( 3944): #91     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #92     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5452:14)
I/flutter ( 3944): #93     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #94     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #95     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #96     ProxyElement.update (package:flutter/src/widgets/framework.dart:4557:5)
I/flutter ( 3944): #97     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #98     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #99     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #100    ProxyElement.update (package:flutter/src/widgets/framework.dart:4557:5)
I/flutter ( 3944): #101    Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #102    RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5161:32)
I/flutter ( 3944): #103    MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5561:17)
I/flutter ( 3944): #104    Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #105    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #106    Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #107    StatefulElement.update (package:flutter/src/widgets/framework.dart:4413:5)
I/flutter ( 3944): #108    Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #109    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #110    Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #111    ProxyElement.update (package:flutter/src/widgets/framework.dart:4557:5)
I/flutter ( 3944): #112    Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #113    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #114    Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #115    StatefulElement.update (package:flutter/src/widgets/framework.dart:4413:5)
I/flutter ( 3944): #116    Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #117    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5452:14)
I/flutter ( 3944): #118    Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #119    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #120    Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #121    StatelessElement.update (package:flutter/src/widgets/framework.dart:4298:5)
I/flutter ( 3944): #122    Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #123    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5452:14)
I/flutter ( 3944): #124    Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #125    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #126    Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #127    StatefulElement.update (package:flutter/src/widgets/framework.dart:4413:5)
I/flutter ( 3944): #128    Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #129    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #130    Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #131    StatefulElement.update (package:flutter/src/widgets/framework.dart:4413:5)
I/flutter ( 3944): #132    Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #133    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #134    Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #135    ProxyElement.update (package:flutter/src/widgets/framework.dart:4557:5)
I/flutter ( 3944): #136    Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #137    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #138    Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #139    ProxyElement.update (package:flutter/src/widgets/framework.dart:4557:5)
I/flutter ( 3944): #140    Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #141    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following RangeError was thrown building TabBar(dirty, dependencies: [_InheritedTheme, _LocalizationsScope-[GlobalKey#6edb0]], state: _TabBarState#2896d):
RangeError (index): Invalid value: Valid value range is empty: 0

The relevant error-causing widget was: 
  TabBar file:///Volumes/data/app/video/lib/home/home.dart:74:16
When the exception was thrown, this was the stack: 
#0      List.[] (dart:core-patch/growable_array.dart:149:60)
#1      _TabBarState.build (package:flutter/src/material/tabs.dart:1017:26)
#2      StatefulElement.build (package:flutter/src/widgets/framework.dart:4334:27)
#3      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4223:15)
#4      Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter ( 3944): #142    Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #143    StatefulElement.update (package:flutter/src/widgets/framework.dart:4413:5)
I/flutter ( 3944): #144    Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
I/flutter ( 3944): #145    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter ( 3944): #146    Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 3944): #147    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2432:33)
I/flutter ( 3944): #148    WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:773:20)
I/flutter ( 3944): #149    RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:283:5)
I/flutter ( 3944): #150    SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1102:15)
I/flutter ( 3944): #151    SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1041:9)
I/flutter ( 3944): #152    SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:957:5)
I/flutter ( 3944): #156    _invoke (dart:ui/hooks.dart:259:10)
I/flutter ( 3944): #157    _drawFrame (dart:ui/hooks.dart:217:3)
I/flutter ( 3944): (elided 3 frames from package dart:async)
I/flutter ( 3944): 
I/flutter ( 3944): ════════════════════════════════════════════════════════════════════════════════════════════════════

 

加载中
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部
返回顶部
顶部