Home > Software engineering >  Flutter TabBar : Issue with controller initialization that appeared after any changes
Flutter TabBar : Issue with controller initialization that appeared after any changes

Time:11-23

Here is my code where the tabBar (that permit me to switch between two pages) is locating :

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

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

  @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,
        bottom: TabBar(
          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(
      new firstpage.ReceptionPage(this.widget.demandesPageModel.listReceptions,
          widget.demandesPageModel.isAgent),
    );
    childrenBox.add(
      new secondpage.EnvoyePage(this.widget.demandesPageModel.listEnvois),
    );

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

Here is the error when I load my page with TabBar :

════════ Exception caught by widgets library ═══════════════════════════════════
The following LateError was thrown building Demandes(dirty, dependencies: [MediaQuery], state: _DemandesState#9071e):
LateInitializationError: Field '_controller@98427185' has not been initialized.

The relevant error-causing widget was
Demandes
lib/…/home/home.dart:89
When the exception was thrown, this was the stack
#0      _DemandesState._controller (package:app_apporteur_affaires/views/home/demandes/demandes.dart)
package:app_apporteur_affaires/…/demandes/demandes.dart:1
#1      _DemandesState._redirectionEnvoyeReception
package:app_apporteur_affaires/…/demandes/demandes.dart:89
#2      _DemandesState.build
package:app_apporteur_affaires/…/demandes/demandes.dart:69
#3      StatefulElement.build
package:flutter/…/widgets/framework.dart:4612
#4      ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4495
...
════════════════════════════════════════════════════════════════════════════════
Reloaded 3 of 857 libraries in 589ms.

This error just appeared today after any changes in this page, I tried to initialized it in many way, but nothing work... In fact, I tried to initialized it in the initState and outside.

CodePudding user response:

the problem is that you used the _controller but you forget to initialize it, you can initialize it in the initState like that :

  @override
  void initState() {
    super.initState();
    _controller = TabController(vsync: this, length: myTabs.length); // myTabs.length change it with number of tabs you have
  }
  • Related