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