Undefined name '_LoginController' Flutter - Dart

Viewed 33

I'm new with flutter and Dart, I'm trying to use a method that's on my controller called LoginController, i already imported it but when i call the instance of my controller in a widget method theres an error that says Undefined name '_LoginController, need some help, pls!

Here my code:

The error message:

Undefined name '_LoginController'. (Documentation)  Try correcting the name to one that is defined, or defining the name.

AndroidStudio suggestions are: enter image description here

Import: import 'package:delivery_app/src/login/login_controller.dart';

Instance on class _LoginPageState:

class _LoginPageState extends State<LoginPage> {

  LoginController _LoginController = new LoginController();

  @override
  void initState() {//Primer metodo que se ejecuta cuando inicial el App
    // TODO: implement initState
    super.initState();

    SchedulerBinding.instance.addPostFrameCallback((timeStamp) {//Inicializa controladores
      _LoginController.init(context);
    });
  }

Widget method where I'm calling my controller instance inside a onTap function:

Widget _rowElement(BuildContext context){
  return Row(
    mainAxisAlignment: MainAxisAlignment.center,
    children: [
      Text(
        'No tienes Cuenta?',
        style: TextStyle(
            color: MyColors.primaryColor
        ),
      ),
      const SizedBox(width: 7,),
      GestureDetector(
        onTap: _LoginController,//<---- THE ERROR IS HERE
        child: Text(
            'Registrate',
            style: TextStyle(
                fontWeight: FontWeight.bold,
                color: MyColors.primaryColor
            )
        ),
      )
    ],
  );
}

My controller:

import 'package:flutter/material.dart';

class LoginController {

  BuildContext? context;

  Future? init(BuildContext context){
    this.context = context;
  }

  void registerPageRedi(BuildContext context){
    Navigator.pushNamed(context, 'register');
  }
}

3 Answers

you cant define controller as the on tap method all you need to do is send the controller data to the function and perform the operation inside the function

Try it

GestureDetector(
  onTap: _LoginController.registerPageRedi(context),
  child: Text(
    'Registrate',
     style: TextStyle(
     fontWeight: FontWeight.bold,
     color: MyColors.primaryColor
   )
  ),
),

Remove void from method

class LoginController {

  BuildContext? context;

  Future? init(BuildContext context){
    this.context = context;
  }

  registerPageRedi(BuildContext context){
    Navigator.pushNamed(context, 'register');
  }
}

I solved it, I moved the instance of LoginController out of the classes, now i can use it globally, although I think it's kinda weird because I was trying to access to the instance in the same class and the instance was still getting unrecognized

LoginController _loginController = new LoginController(); //Instance out of classes
    
class LoginPage extends StatefulWidget {
    
}
    
class _LoginPageState extends State<LoginPage> {

}

Thanks to those who tried to help me :D

Related