Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter TabBar : Issue with controller initialization that appeared after any changes

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading