"GetXController" not found. You need to call?

121
  1. i have added the detail code for binding controller and increment function so try using this, it will definitely work
void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      initialBinding: UserBinding(),
      getPages: [...routeList],
      initialRoute: RouteNames.welcomePage,
      home: const Scaffold(
        backgroundColor: Color.fromRGBO(244, 248, 252, 1),
      ),
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primaryColor: const Color(0XFFFB6394),
        appBarTheme: const AppBarTheme(
          systemOverlayStyle: SystemUiOverlayStyle.dark,
        ),
      ),
    );
  }
}

class UserController extends GetxController {
  var i = 0.obs;
  void increment(){
    i++;
  }
}
class RouteNames {
  static String welcomePage = "/welcomePage";
}
List routeList = [
  GetPage(
      name: '/${RouteNames.welcomePage}',
      page: () =>  WelcomePage(),
      ),
];
//here use binding like this 
class UserBinding implements Bindings {
  @override
  void dependencies() {
    Get.put<UserController>(UserController(), permanent: true);
  }
}
class WelcomePage extends StatelessWidget {
   WelcomePage({Key? key}) : super(key: key);
  // final userController = Get.put(UserController());
   final userController  = Get.find<UserController>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
      ),
      body: Column(
        children: [
         Obx(() => Text(userController.i.value.toString(),
         style: const TextStyle(
           fontSize: 25,
           color: Colors.blueAccent
         ),),),
          ElevatedButton(onPressed: (){
            userController.increment();
          }, child: const Text("Button")),
        ],
      ),
    );
  }
}

Share:
121
Milosh N.
Author by

Milosh N.

Updated on January 04, 2023

Comments

  • Milosh N.
    Milosh N. over 1 year

    I want to implement GetX binding methods, and as result, I got this error:

    ════════ Exception caught by widgets library ═══════════════════════════════════
    The following message was thrown building MyApp(dirty):
    "UserController" not found. You need to call "Get.put(UserController())" or "Get.lazyPut(()=>UserController())"
    
    The relevant error-causing widget was
    MyApp
    lib/main.dart:9
    
    main.dart
    
    import 'package:donirajkrv/views/welcome.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    import 'package:get/get.dart';
    import './router/index.dart';
    import './bindings/user_binding.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
      @override
      Widget build(BuildContext context) {
        return GetMaterialApp(
          initialBinding: UserBinding(),
          home: const Scaffold(
            backgroundColor: Color.fromRGBO(244, 248, 252, 1),
            body: SafeArea(child: WelcomePage()),
          ),
          getPages: [...routeList],
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
            primaryColor: const Color(0XFFFB6394),
            appBarTheme: const AppBarTheme(
              systemOverlayStyle: SystemUiOverlayStyle.dark,
            ),
          ),
        );
      }
    }
    
    

    How main works:

    
    void main() {
      Get.put<UserController>(UserController());
      runApp(const MyApp());
    }
    
    user_binding.dart
    import 'package:get/get.dart';
    import 'package:donirajkrv/controllers/user_controller.dart';
    
    class UserBinding implements Bindings {
      @override
      void dependencies() {
        Get.put(() => UserController());
      }
    }
    
    

    Routelist:

    import 'package:get/get.dart';
    import '../views/welcome.dart';
    import '../views/login.dart';
    import '../views/register.dart';
    import '../views/home_page.dart';
    import '../middlewares/auth_middleware.dart';
    import '../middlewares/back_to_home_middleware.dart';
    
    // Routes path
    import '../routes/index.dart';
    
    List routeList = [
      GetPage(
          name: '/${Routes.WELCOME_PAGE}',
          page: () => const WelcomePage(),
          middlewares: [BackToHomeMiddleware()]),
      GetPage(
          name: '/${Routes.LOGIN_PAGE}',
          page: () => const Login(),
          middlewares: [BackToHomeMiddleware()]),
      GetPage(
          name: '/${Routes.REGISTER_PAGE}',
          page: () => const Register(),
          middlewares: [BackToHomeMiddleware()]),
      GetPage(
          name: '/${Routes.HOME_PAGE}',
          page: () => const HomePage(),
          middlewares: [AuthMiddleware()])
    ];
    
    
  • Milosh N.
    Milosh N. about 2 years
    Not working :( initialBinding: UserBinding() and I want for example controller in middleware final authService = Get.find<UserController>().isUserLogged;, won't work, again same error.
  • Prodromos Sarakinou
    Prodromos Sarakinou about 2 years
    still the same error?
  • Milosh N.
    Milosh N. about 2 years
    Yes. For example, welcome screen : class WelcomePageState extends State<WelcomePage> { final isUserLogged = Get.find<UserController>().isUserLogged;
  • Milosh N.
    Milosh N. about 2 years
    @Prodomos, done!
  • Prodromos Sarakinou
    Prodromos Sarakinou about 2 years
    @MiloshN. Unfortunately, now I need the routesList too :P, in the meantime, I am developing an example for getx bindings
  • Milosh N.
    Milosh N. about 2 years
    @Prodomos, done :D
  • Prodromos Sarakinou
    Prodromos Sarakinou about 2 years
    @MiloshN. I've just added a github example, because I couldn't find where the error happens... Please check it. Be careful with the use of the null-aware operators, for example, I declare the UserController like this: UserController? controller and it didn't work, I got an error like yours.
  • Sumed
    Sumed about 2 years
    please upvote and accept the answer if its helps you