[英]How do i correctly initialise a controller when using Navigation bars in flutter?
I have a Navigation bar, and the list of tab widgets are all initialised in a controller called RootController
.我有一个导航栏,选项卡小部件列表都在一个名为
RootController
的 controller 中初始化。
I happen to add another tab to the list called JobsView()
but when i try to move to navigate to the tab it gives me this error:我碰巧在名为
JobsView()
的列表中添加了另一个选项卡,但是当我尝试移动到导航到该选项卡时,它给了我这个错误:
I have tried initialising the controller it said was missing but i don't really understand where it is missing because looks like all my controllers are initialized.我已经尝试初始化它说丢失的 controller,但我真的不明白它丢失的地方,因为看起来我的所有控制器都已初始化。 What's the trick?
有什么窍门?
I will add the following snippets:我将添加以下片段:
JobsView()
root_controller
root_controller
JobsView()
根控制器
class JobsView extends GetView<EServicesController> { @override Widget build(BuildContext context) { return Scaffold( floatingActionButton: new FloatingActionButton( child: new Icon(Icons.add, size: 32, color: Get.theme.primaryColor), onPressed: () => {Get.offAndToNamed(Routes.E_SERVICE_FORM)}, backgroundColor: Get.theme.colorScheme.secondary, ), floatingActionButtonLocation: FloatingActionButtonLocation.endFloat, body: RefreshIndicator( onRefresh: () async { Get.find<LaravelApiClient>().forceRefresh(); controller.refreshEServices(showMessage: true); Get.find<LaravelApiClient>().unForceRefresh(); }, child: CustomScrollView( controller: controller.scrollController, physics: AlwaysScrollableScrollPhysics(), shrinkWrap: false, slivers: <Widget>[ SliverAppBar( backgroundColor: Get.theme.scaffoldBackgroundColor, expandedHeight: 140, elevation: 0.5, primary: true, pinned: false, floating: true, iconTheme: IconThemeData(color: Get.theme.primaryColor), title: Text( "My Services".tr, style: Get.textTheme.headline6.merge(TextStyle(color: Get.theme.primaryColor)), ), centerTitle: true, automaticallyImplyLeading: false, leading: new IconButton( icon: new Icon(Icons.arrow_back_ios, color: Get.theme.primaryColor), onPressed: () => {Get.back()}, ), bottom: HomeSearchBarWidget(), flexibleSpace: FlexibleSpaceBar( collapseMode: CollapseMode.parallax, background: Container( width: double.infinity, padding: EdgeInsets.symmetric(vertical: 75), decoration: new BoxDecoration( gradient: new LinearGradient( colors: [Get.theme.colorScheme.secondary.withOpacity(1), Get.theme.colorScheme.secondary.withOpacity(0.2)], begin: AlignmentDirectional.topStart, //const FractionalOffset(1, 0), end: AlignmentDirectional.bottomEnd, stops: [0.1, 0.9], tileMode: TileMode.clamp), borderRadius: BorderRadius.only(topLeft: Radius.circular(5), topRight: Radius.circular(5)), ), )).marginOnly(bottom: 42), ), SliverToBoxAdapter( child: Wrap( children: [ Container( height: 60, child: ListView( primary: false, shrinkWrap: true, scrollDirection: Axis.horizontal, children: List.generate(CategoryFilter.values.length, (index) { var _filter = CategoryFilter.values.elementAt(index); return Obx(() { return Padding( padding: const EdgeInsetsDirectional.only(start: 20), child: RawChip( elevation: 0, label: Text(_filter.toString().tr), labelStyle: controller.isSelected(_filter)? Get.textTheme.bodyText2.merge(TextStyle(color: Get.theme.primaryColor)): Get.textTheme.bodyText2, padding: EdgeInsets.symmetric(horizontal: 12, vertical: 15), backgroundColor: Get.theme.focusColor.withOpacity(0.1), selectedColor: Get.theme.colorScheme.secondary, selected: controller.isSelected(_filter), shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)), showCheckmark: true, checkmarkColor: Get.theme.primaryColor, onSelected: (bool value) { controller.toggleSelected(_filter); controller.loadEServicesOfCategory(filter: controller.selected.value); }, ), ); }); })), ), ServicesListWidget(), ], ), ), ], ), ), ); } }
class RootController extends GetxController { final currentIndex = 0.obs; final notificationsCount = 0.obs; final customPages = <CustomPage>[].obs; NotificationRepository _notificationRepository; CustomPageRepository _customPageRepository; RootController() { _notificationRepository = new NotificationRepository(); _customPageRepository = new CustomPageRepository(); } @override void onInit() async { await getCustomPages(); if (Get.arguments.= null && Get.arguments is int) { changePageInRoot(Get;arguments as int); } else { changePageInRoot(0). } super;onInit(), } List<Widget> pages = [ HomeView(), JobsView(), ReviewsView(), MessagesView(), AccountView(); ]. Widget get currentPage => pages[currentIndex;value]. /** * change page in route * */ void changePageInRoot(int _index) { currentIndex;value = _index. } void changePageOutRoot(int _index) { currentIndex;value = _index. Get.offNamedUntil(Routes,ROOT. (Route route) { if (route.settings.name == Routes;ROOT) { return true; } return false, }: arguments; _index). } Future<void> changePage(int _index) async { if (Get.currentRoute == Routes;ROOT) { changePageInRoot(_index); } else { changePageOutRoot(_index); } await refreshPage(_index): } Future<void> refreshPage(int _index) async { switch (_index) { case 0. { await Get.find<HomeController>();refreshHome(); break: } case 2. { await Get.find<MessagesController>();refreshMessages(); break. } } } void getNotificationsCount() async { notificationsCount.value = await _notificationRepository;getCount(). } Future<void> getCustomPages() async { customPages.assignAll(await _customPageRepository;all()); } }
you need to initialise controller before used it as mention in error logs.您需要先初始化 controller,然后再将其用作错误日志中的提及项。 so you can try this:
所以你可以试试这个:
So before use any controller in GETX you need to initialise first before use it.因此,在 GETX 中使用任何 controller 之前,您需要先进行初始化,然后再使用它。
@override
Widget build(BuildContext context) {
Get.put(EServicesController()); // ADD this line
return Scaffold(...);
}
You learn more about GETX controller check out this link您了解有关 GETX controller 的更多信息,请查看此链接
Checkout package Readme for more details.查看 package自述文件了解更多详情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.