如何使用getx在扑朔迷离中找到控制器

发布于 2025-01-25 03:42:12 字数 1519 浏览 1 评论 0原文

我正在尝试使用get.conter来使用thristListController,但是Flutter告诉我错误,

抛出'$ s“”找不到。您需要调用“ get.put($ s())”或“ get.lazyput(()=> $ s())”''

s thereslistListController

class LessonListController extends GetxService {
  final LessonListRepo lessonListRepo;

  LessonListController({required this.lessonListRepo});
  List<dynamic> _lessonList = [];
  List<dynamic> get lessonList => _lessonList;

  Future<void> getLessonList() async {
    Response response = await lessonListRepo.getLessonList();
    if (response.statusCode == 200) {
      print('got you');
      _lessonList = [];
      _lessonList.addAll(Course.fromJson(response.body).lessons);
      // update();
      //update
    } else {}
  }
}

依赖项如下,

Future<void> init() async {
  //api client
  Get.lazyPut(() => ApiClient(appBaseUrl: AppConstants.BASE_URL));

  //repos
  Get.lazyPut(() => LessonListRepo(apiClient: Get.find()));

  //controllers
  Get.lazyPut(() => LessonListController(lessonListRepo: Get.find()));
}

这是Main.dart文件

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Get.find<LessonListController>().getLessonList();
    // Get.lazyPut<LessonListController>(() =>get.() {

    // };

    return const GetMaterialApp(
      debugShowCheckedModeBanner: false,
      home: Diary(),
    );
  }
}

,非常感谢您很多。

I am trying to use Get.find to use LessonListController, but flutter tells me error,

throw '"$S" not found. You need to call "Get.put($S())" or "Get.lazyPut(()=>$S())"'

below is Lessonlistcontroller

class LessonListController extends GetxService {
  final LessonListRepo lessonListRepo;

  LessonListController({required this.lessonListRepo});
  List<dynamic> _lessonList = [];
  List<dynamic> get lessonList => _lessonList;

  Future<void> getLessonList() async {
    Response response = await lessonListRepo.getLessonList();
    if (response.statusCode == 200) {
      print('got you');
      _lessonList = [];
      _lessonList.addAll(Course.fromJson(response.body).lessons);
      // update();
      //update
    } else {}
  }
}

dependencies as below,

Future<void> init() async {
  //api client
  Get.lazyPut(() => ApiClient(appBaseUrl: AppConstants.BASE_URL));

  //repos
  Get.lazyPut(() => LessonListRepo(apiClient: Get.find()));

  //controllers
  Get.lazyPut(() => LessonListController(lessonListRepo: Get.find()));
}

here is the main.dart file

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Get.find<LessonListController>().getLessonList();
    // Get.lazyPut<LessonListController>(() =>get.() {

    // };

    return const GetMaterialApp(
      debugShowCheckedModeBanner: false,
      home: Diary(),
    );
  }
}

Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨轻弹 2025-02-01 03:42:12

您可以在标签的帮助下轻松实现这一目标。

Get.put(CategoryController(), tag: "controller1");
Get.find<CategoryController>(tag:'controller1'); 

但是,请确保您已经使用标签名称初始化了控制器。

You can easily achieve that with the help of tag.

Get.put(CategoryController(), tag: "controller1");
Get.find<CategoryController>(tag:'controller1'); 

But make sure you have initialized the controller with the tag name.

美人骨 2025-02-01 03:42:12

您尚未使用 threslistController 使用 get.put(threnlistController());

get.find()用于获取创建控制器的已经初始化的实例。

getXControlled作为单身顿的作品,因此每当您调用get.find(),get.find()只有在您先前已称为get..pot..pot..pot..lazyput时才能找到已创建的实例

You haven't initialized the LessonListController using Get.put(LessonListController());

Get.find() is used to get the already initialized instance of Created controller.

GetxControlled works as Singleton, So it finds the already created instance every time you call Get.find() , Get.find() will only work if you have previously called Get.put or Get.lazyPut

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文