Home > database >  not found. You need to call "Get.put(FoodController())" or "Get.lazyPut(()=>FoodCo
not found. You need to call "Get.put(FoodController())" or "Get.lazyPut(()=>FoodCo

Time:10-23

I'm creating routes with bindings inside my application , but whenever i navigate from main screen to the home scree, it throws an exception that i need to initialize the controller which what i already did in the homebinding class , i need some help thank you .

  • This is the error throwin

 The following message was thrown building HomeScreen(dirty):
"FoodController" not found. You need to call "Get.put(FoodController())" or "Get.lazyPut(()=>FoodController())"

  • This is my main screen
void main() {
  runApp(GetMaterialApp(
    home: const MyApp(),
    debugShowCheckedModeBanner: false,
    initialBinding: HomeBinding(),
    getPages: [
      // using HomeBinding() class for home and details screen since i'm going to use the same controller for both
      GetPage(name: '/', page: () => const MyApp()),
      GetPage(name: '/home', page:  () => const HomeScreen(),binding: HomeBinding()),
      GetPage(name: '/details', page: () => const DetailsScreen(), binding: HomeBinding())
    ],
  ));
}


class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return  const HomeScreen();
  }
}
  • This is my home binding class

 class HomeBinding extends Bindings {
  @override
  void dependencies() {
     Get.lazyPut(() => () => FoodController());
  }

}

CodePudding user response:

Try replacing

Get.lazyPut(() => () => FoodController());

by

Get.lazyPut(() => FoodController())

You can also specify the Type with

Get.lazyPut<FoodController>(() => FoodController())
  • Related