【问题标题】:getx: AddController not found. You need to call Get.put(AddController())getx:未找到 AddController。您需要调用 Get.put(AddController())
【发布时间】:2021-09-28 07:03:41
【问题描述】:

我在我的项目中使用 getx,我正在尝试使用 getx 依赖注入。我创建 AddBinding 类:

class AddBinding implements Bindings {
  @override
  void dependencies() {
    Get.lazyPut<AddController>(
      () => AddController(
        AddRepository(),
        Get.find<DialogService>(),
      ),
    );
  }
}

GetPage 中,我添加了这样的绑定:

GetPage(
  name: Routes.ADD,
  page: () => AddPage(),
  binding: AddBinding(),
),

现在在我的主页上

class HomePage extends GetView<HomeController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // title: const Text('HomePage'),
        title: TabBar(
          controller: controller.controller,
          tabs: controller.myTabs,
        ),
      ),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: TabBarView(
          controller: controller.controller,
          physics: const NeverScrollableScrollPhysics(),
          children: [
            Container(),
            Container(),
            Container(),
            AddPage(),
          ],
        ),
      ),
    );

我添加了我的 AddPage。这是添加页面:

class AddPage extends GetView<AddController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(child: Container()),
      floatingActionButton: FloatingActionButton(
        elevation: 0.0,
        backgroundColor: const Color(0xFFE57373),
        onPressed: () async {
          controller.showAddDialog();
        },
        child: const Icon(Icons.add),
      ),

    );
  }
}

但是当我点击 fab controller.showAddDialog(); 时出现错误: E/flutter (26699): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: "AddController" not found. You need to call "Get.put(AddController())" or "Get.lazyPut(()=&gt;AddController())" E/flutter (26699): #0 GetInstance.find package:get/…/src/get_instance.dart:332 E/flutter (26699): #1 Inst.find package:get/…/src/extension_instance.dart:70

这是 AddController:

class AddController extends GetxController {
  final AddRepository repository;
  final DialogService dialogService;
  AddController(this.repository, this.dialogService);

  void showAddDialog() {

  }
}

什么问题?

这是 MaterialApp:

GetMaterialApp(
        debugShowCheckedModeBanner: false,
        initialRoute: Routes.INITIAL,
        theme: appThemeData,
        defaultTransition: Transition.fade,
        initialBinding: MainBinding(),
        locale: const Locale('fa', 'IR'),
        translationsKeys: AppTranslation.translations,
        getPages: AppPages.pages,
      ),
    ),
  );

【问题讨论】:

    标签: flutter flutter-dependencies flutter-getx


    【解决方案1】:

    您没有使用 GetX 导航系统导航到 AddPage。您正在做的是从同一页面(主页)切换选项卡。因此AddBinding 类对其没有影响。仅当您使用Get.toNamed(Routes.ADD)Get.to(()=&gt;AddPage(), binding: AddBinding()) 导航时才会生效。

    一种解决方法是将您的 AddBinding 依赖项放到标签父项(主页)绑定中,您将 HomeController 放在其中

    【讨论】:

    • 在tabview中我们不使用Get.to所以我只是把AddController放在mainBinding里面?这只是解决方案?@S。 M.贾汉吉尔
    • 是的!显然这是最简单的解决方案。
    猜你喜欢
    • 2021-02-26
    • 2021-11-05
    • 2021-12-27
    • 2022-12-16
    • 1970-01-01
    • 2021-12-11
    • 2021-04-03
    • 2021-04-09
    • 2021-07-19
    相关资源
    最近更新 更多