Home > OS >  Undefined name '_LoginController' Flutter - Dart
Undefined name '_LoginController' Flutter - Dart

Time:09-20

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');
  }
}

CodePudding user response:

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

CodePudding user response:

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');
  }
}
  • Related