You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter TabBar控制器LateInitializationError初始化报错求助

Flutter TabBar 控制器初始化错误解决方案

错误根因

你遇到的LateInitializationError错误核心有两个问题:

  1. 代码中声明了late TabController _controller但没有重写initState生命周期方法完成初始化,页面构建时TabBarView访问未赋值的控制器触发报错
  2. 现有代码仅给TabBarView绑定了控制器,TabBar组件未指定controller属性,即使控制器初始化完成也会出现标签和页面无法联动的问题

修复代码

你需要补充initState初始化逻辑,同时给TabBar添加控制器绑定,完整修正后代码如下:

class Demandes extends StatefulWidget {
  final DemandesPageModel demandesPageModel;
  Demandes(this.demandesPageModel);
  @override
  _DemandesState createState() => _DemandesState();
}

class _DemandesState extends State<Demandes>
    with SingleTickerProviderStateMixin {
  late TabController _controller;

  // 新增initState方法初始化控制器
  @override
  void initState() {
    super.initState();
    // length参数和Tab数量保持一致,这里是2个标签所以设为2
    _controller = TabController(length: 2, vsync: this);
  }

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

  @override
  Widget build(BuildContext context) {
    var mediaQueryData = MediaQuery.of(context);
    final height = mediaQueryData.size.height;
    return Scaffold(
      backgroundColor: backgroundColor,
      appBar: AppBar(
        automaticallyImplyLeading: false,
        elevation: 0,
        title: Text(
          "Demandes",
          style: TextStyle(
              color: Colors.black, fontSize: 20, fontWeight: FontWeight.w600),
        ),
        backgroundColor: backgroundColor,
        // 给TabBar补充controller参数
        bottom: TabBar(
          controller: _controller,
          indicatorColor: blueColor,
          indicatorSize: TabBarIndicatorSize.label,
          tabs: <Widget>[
            Tab(
                child: Text(
              "Réception",
              style: TextStyle(
                  color: Colors.black,
                  fontSize: 20,
                  fontWeight: FontWeight.w400),
            )),
            Tab(
                child: Text("Envoyées",
                    style: TextStyle(
                        color: Colors.black,
                        fontSize: 20,
                        fontWeight: FontWeight.w400))),
          ],
        ),
      ),
      body: Container(
        margin: EdgeInsets.only(
          bottom: height / 8,
        ),
        child: _redirectionEnvoyeReception(),
      ),
      bottomSheet: BlocProvider.value(
        value: BlocProvider.of<NavigateHomeScreenBloc>(context),
        child: AddCollaboratorButton(),
      ),
    );
  }

  Widget _redirectionEnvoyeReception() {
    final childrenBox = <Widget>[];
    childrenBox.add(
      firstpage.ReceptionPage(this.widget.demandesPageModel.listReceptions,
          widget.demandesPageModel.isAgent),
    );
    childrenBox.add(
      secondpage.EnvoyePage(this.widget.demandesPageModel.listEnvois),
    );

    return TabBarView(
      controller: _controller,
      children: childrenBox,
    );
  }
}

验证说明

修复后热重载即可解决报错,Tab标签和页面可以正常联动切换,原有业务逻辑无需额外修改。

内容的提问来源于stack exchange,提问作者Dev Lulu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 01:36:02