Getx: LoginController not found. You need call Get.put<LoginController>(LoginController()) before

Created on 10 Jun 2020  路  5Comments  路  Source: jonataslaw/getx

Hi

If we reference the controller using the final outside build method in Login class like this

final c = LoginController.to;

then an error occurred.

LoginController not found. You need call Get.put(LoginController()) before

And it's working if we just call it via LoginController.to.property.

Is it normal?

Thanks

import 'package:flutter/material.dart';
import 'package:get/get.dart';

void main() {
  runApp(App());
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      initialRoute: '/login',
      namedRoutes: {
        '/login': GetRoute(page: Login(), binding: LoginBinding()),
      },
    );
  }
}

class Login extends StatelessWidget {
  final c = LoginController.to; // make reff

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        padding: EdgeInsets.all(48.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextField(
              controller: c.phoneController,
//              controller: LoginController.to.phoneController, // It's working
            )
          ],
        ),
      ),
    );
  }
}

class LoginBinding extends Bindings {
  @override
  void dependencies() {
    Get.lazyPut<LoginController>(() => LoginController());
  }
}

class LoginController extends GetController {
  static LoginController get to => Get.find();

  final TextEditingController phoneController = TextEditingController();
}

Most helpful comment

Yep, LoginBinding doesn't get called until you're in the widget. Being pedantic, I think you should just implementBindings, not extendBindings.
Enjoy.

All 5 comments

Please don't do this anymore. You will make your application highly unstable. Follow the steps in the documentation.

LoginController c = Get.find();

You would only use the statistical method if you intended to always access it directly. You can remove the static method and use it that way if you prefer.
And for now, I recommend that you put it inside the build method. It is not good to create instances within the build method, but believe me, Get.find() will not create an instance, it will only point to an object in memory, so it is safe enough.

Thanks for make it clear. Really appreciate it.

Cheers

>

Yep, LoginBinding doesn't get called until you're in the widget. Being pedantic, I think you should just implementBindings, not extendBindings.
Enjoy.

I also think it's good to highlight why:
When you use final, and try to access a static instance, you are sending a dynamic as a type, and this can cause problems after compilation. That way, you either access the object directly, which will force the static variable to type the object, or you use it within your view, preferably in the build method, as I gave the example.
Other approaches may work sometimes, but it will not always be so, in addition to being totally unsafe for your application.
As the instance will only be created when the build renders the widget that uses it, at least for now, I do not recommend using Binding and final variables in a stateless, because you will have to use the constructor, and the moment you do GetRoute(page : Home()) you are calling the constructor, so the instance will be called before it is created, and it can also cause problems, so I recommend (at least for now, I intend to give Bindings another approach in the future) to use this way , within the same build method, or directly access the widget.

Thank you so much for pointing me out :+1:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wailashraf71 picture wailashraf71  路  4Comments

rupamking1 picture rupamking1  路  3Comments

GoldenSoju picture GoldenSoju  路  3Comments

manojeeva picture manojeeva  路  3Comments

R-Praveen picture R-Praveen  路  4Comments