Flutter TabBar:任何更改后出现的控制器初始化问题



这是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;
@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,
);
}
}

这是我用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.

这个错误今天刚刚出现,在这个页面发生任何更改后,我试图用多种方式初始化它,但都不起作用。。。事实上,我试图在initState和外部初始化它。

问题是您使用了_controller,但忘记了初始化它,您可以在initState中初始化它,如下所示:

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

最新更新