getx: AddController not found. You need to call Get.put(AddController())

Issue

I am using getx to my project, I am trying to use getx dependency injection. I create AddBinding class:

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

And In GetPage I added this binding like :

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

Now In my Home page

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(),
          ],
        ),
      ),
    );

I add My AddPage. This is add page:

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),
      ),

    );
  }
}

But when I clicked on fab controller.showAddDialog(); I got error:
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(()=>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

This is AddController:

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

  void showAddDialog() {

  }
}

What is problem?

This is 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,
      ),
    ),
  );

Solution

You are not navigating to AddPage using the GetX navigation system. What you are doing is switching tabs from the same page (HomePage). Therefore the AddBinding class has no effects on it. It will only have effect if you were navigating with Get.toNamed(Routes.ADD) or Get.to(()=>AddPage(), binding: AddBinding()).

One workaround could be put your AddBinding dependencies to the tabs parents (HomePage) binding, where you putting your HomeController

Answered By – S. M. JAHANGIR

Answer Checked By – Cary Denson (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *