[英]Undefined name '_LoginController' Flutter - Dart
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 ,需要一些幫助,請!
這是我的代碼:
錯誤信息:
Undefined name '_LoginController'. (Documentation) Try correcting the name to one that is defined, or defining the name.
導入: import 'package:delivery_app/src/login/login_controller.dart';
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);
});
}
我在 onTap function 中調用 controller 實例的小部件方法:
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
)
),
)
],
);
}
我的 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');
}
}
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
試試看
GestureDetector(
onTap: _LoginController.registerPageRedi(context),
child: Text(
'Registrate',
style: TextStyle(
fontWeight: FontWeight.bold,
color: MyColors.primaryColor
)
),
),
從方法中刪除 void
class LoginController {
BuildContext? context;
Future? init(BuildContext context){
this.context = context;
}
registerPageRedi(BuildContext context){
Navigator.pushNamed(context, 'register');
}
}
我解決了它,我將 LoginController 的實例移出類,現在我可以在全局范圍內使用它,盡管我認為這有點奇怪,因為我試圖訪問同一個 class 中的實例並且實例仍然無法識別
LoginController _loginController = new LoginController(); //Instance out of classes
class LoginPage extends StatefulWidget {
}
class _LoginPageState extends State<LoginPage> {
}
感謝那些試圖幫助我的人:D
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.