[英]Flutter Getx - "Xxx" not found. You need to call "Get.put(Xxx())" - But i have called Get.put(Xxx())
[英]Getx controller not found even after Get.put() | Flutter | Getx
"YouController" not found. You need to call "Get.put(YouController())" or
"Get.lazyPut(()=>YouController())"
即使在进入屏幕之前我有 Get.put() 之后也会出现此错误。
任何的想法?
你可以试试这个:
在您的 lib 文件夹中创建一个名为initial_bindings.dart
的文件。 然后将此代码段添加到其中。
import 'package:get/get.dart';
class InitialBinding implements Bindings {
@override
void dependencies() {
Get.lazyPut<YouController>(() => YouController(),fenix: true);
}
}
然后在您的main.dart
中,将此行添加到您的GetMaterialApp
中。
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return GetMaterialApp(
initialBinding: InitialBinding(), // add this line
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
然后,无论何时你想访问 controller,你都可以使用Get.find<YourController>()
来使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.