简体   繁体   English

在 flutter 中使用导航栏时,如何正确初始化 controller?

[英]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.

相关问题 我如何正确地将变量从有状态小部件初始化为 flutter 中的另一个小部件并维护 state? - How do i correctly initialise a variable from a stateful widget to another widget in flutter and maintain state? 我们什么时候在 flutter 中初始化一个提供者? - When do we initialise a provider in flutter? 使用 Flutter 的 Cupertino 小部件,我如何触发底部标签栏顶部的水平页面转换 - Using Flutter's Cupertino widgets, how do I trigger a horizontal page transition on top of bottom tab bars 如何初始化列表? - How do I initialise a List? 多个导航栏颤动 - Multiple Navigation bars flutter How to call a swift view controller using the navigation controller in flutter through the Flutter Method Channel - How to call a swift view controller using the navigation controller in flutter through the Flutter Method Channel 使用 charts_flutter 包时,如何使用从提供程序文件获取的值设置条形的颜色 - How can i set the color of the bars with the values gotten from a provider file when using charts_flutter package 将 Provider 与 TickerProviderStateMixin 一起使用:我在哪里可以初始化我的 controller? - Using Provider with with TickerProviderStateMixin: Where can I initialise my controller? Flutter:如何从导航正确返回? - Flutter : how to return correctly from navigation? 如何在 Flutter 中使用 GetX 观察 GetxController 类中的值变化并触发 statelessWidget 类中的导航? - How do I observe a value change in a GetxController class and trigger navigation in the statelessWidget class using GetX in Flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM