未找到 GetX Controller,需要调用 Get.put 或 Get.lazyPut

发布于 2025-01-09 21:05:56 字数 3059 浏览 1 评论 0原文

当我导航到 SettingsPage 并刷新页面时,它会抛出下图中所示的错误。我似乎不知道如何解决它。设置(到目前为止):

ma​​in文件中:

Future<void> main() async {
 

  Get.put(UserService(httpClient), permanent: true);
  Get.put(ProjectService(httpClient), permanent: true);
  Get.put(TaskService(httpClient), permanent: true);
  runApp(MyApp());
}

然后:

class MyAppextends StatelessWidget {
  @override
  Widget build(BuildContext context) {
   
    return GetMaterialApp(
      initialBinding: BindingsBuilder<AuthController>.put(
        () => AuthController(),
        permanent: true,
      ),
      debugShowCheckedModeBanner: false,
      initialRoute: BoardPage.route,
      getPages: pages,
      defaultTransition: Transition.fadeIn,
    );
  }
}

pages是我的全局绑定文件,它在我的GetMaterialApp中初始化

final List<GetPage<dynamic>> pages = <GetPage<dynamic>>[

  GetPage<dynamic>(
    name: OnBoardingPage.route,
    page: () => OnBoardingPage(),
  ),
  GetPage<dynamic>(
    name: RegisterPage.route,
    page: () => RegisterPage(),
    binding: BindingsBuilder<RegisterController>.put(
      () => RegisterController(),
    ),
  ),
  GetPage<dynamic>(
    name: LoginPage.route,
    page: () => LoginPage(),
    binding: BindingsBuilder<LoginController>.put(
      () => LoginController(),
    ),
  ),
  GetPage<dynamic>(
    name: BoardPage.route,
    page: () => BoardPage(),
    bindings: <Bindings>[
      BindingsBuilder<TaskController>.put(() => TaskController()),
      BindingsBuilder<BoardController>.put(() => BoardController()),
    ],
  ),
  GetPage<dynamic>(
    name: SettingsPage.route,
    page: () => SettingsPage(),
    bindings: <Bindings>[
      // TODO(dakr): remove for release
      BindingsBuilder<BoardController>.put(() => BoardController()),
      BindingsBuilder<SettingsController>.put(() => SettingsController()),
      BindingsBuilder<TaskController>.put(() => TaskController()),
    ],
  ),
  GetPage<dynamic>(
    name: ProfilePage.route,
    page: () => ProfilePage(),
    binding: BindingsBuilder<ProfileController>.put(
          () => ProfileController(),
    ),
  ),
];

,出现错误当我尝试刷新设置页面时。我认为使用 Obx 存在问题,但即使我只返回彩色容器,它也会显示错误。

设置页面

class SettingsPage extends GetView<SettingsController> { 
    static const String route = '/board/settings'; 
    final BoardController _boardController = Get.find(); 
    final ProjectService _projectService = Get.find(); 
    final AuthController _authController = Get.find();

     @override Widget build(BuildContext context) { 
        return Scaffold( body: Container(), ); 
      } 
}

在此处输入图像描述

When I navigate to my SettingsPage and refresh the page it throws the error seen in the image below. I cant seem to figure out how to solve it. The setup (so far):

In the main file:

Future<void> main() async {
 

  Get.put(UserService(httpClient), permanent: true);
  Get.put(ProjectService(httpClient), permanent: true);
  Get.put(TaskService(httpClient), permanent: true);
  runApp(MyApp());
}

Then:

class MyAppextends StatelessWidget {
  @override
  Widget build(BuildContext context) {
   
    return GetMaterialApp(
      initialBinding: BindingsBuilder<AuthController>.put(
        () => AuthController(),
        permanent: true,
      ),
      debugShowCheckedModeBanner: false,
      initialRoute: BoardPage.route,
      getPages: pages,
      defaultTransition: Transition.fadeIn,
    );
  }
}

pages is my global bindings file which is initialized in my GetMaterialApp

final List<GetPage<dynamic>> pages = <GetPage<dynamic>>[

  GetPage<dynamic>(
    name: OnBoardingPage.route,
    page: () => OnBoardingPage(),
  ),
  GetPage<dynamic>(
    name: RegisterPage.route,
    page: () => RegisterPage(),
    binding: BindingsBuilder<RegisterController>.put(
      () => RegisterController(),
    ),
  ),
  GetPage<dynamic>(
    name: LoginPage.route,
    page: () => LoginPage(),
    binding: BindingsBuilder<LoginController>.put(
      () => LoginController(),
    ),
  ),
  GetPage<dynamic>(
    name: BoardPage.route,
    page: () => BoardPage(),
    bindings: <Bindings>[
      BindingsBuilder<TaskController>.put(() => TaskController()),
      BindingsBuilder<BoardController>.put(() => BoardController()),
    ],
  ),
  GetPage<dynamic>(
    name: SettingsPage.route,
    page: () => SettingsPage(),
    bindings: <Bindings>[
      // TODO(dakr): remove for release
      BindingsBuilder<BoardController>.put(() => BoardController()),
      BindingsBuilder<SettingsController>.put(() => SettingsController()),
      BindingsBuilder<TaskController>.put(() => TaskController()),
    ],
  ),
  GetPage<dynamic>(
    name: ProfilePage.route,
    page: () => ProfilePage(),
    binding: BindingsBuilder<ProfileController>.put(
          () => ProfileController(),
    ),
  ),
];

The error appears when I try to refresh the SettingsPage. I thought that there was a problem using Obx but even when I only return a colored Container it shows the error.

SettingsPage:

class SettingsPage extends GetView<SettingsController> { 
    static const String route = '/board/settings'; 
    final BoardController _boardController = Get.find(); 
    final ProjectService _projectService = Get.find(); 
    final AuthController _authController = Get.find();

     @override Widget build(BuildContext context) { 
        return Scaffold( body: Container(), ); 
      } 
}

enter image description here

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

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

发布评论

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

评论(1

妖妓 2025-01-16 21:05:56

尝试正常的

class YourBind extends Binding{

  @override
  void dependencies() {
    // TODO: implement dependencies
     Get.lazyPut(() => TaskController(), fenix: true);
}

then 对于 GetPage

class RouteList{

  class RouteList {
  static final routeLists = [
    GetPage(
        transition: Transition.cupertinoDialog,
        transitionDuration: 350.milliseconds,
        binding: YourBind(),
        name: "/home",
        page: () => const SettingPage()),
  ];
}

}

then 对于控制器

class SettingsController extends GetxController{
  final youController = Get.find<TaskController>();
 
}

来说下一个是

class SettingsPage extends GetView<SettingsController> { 
     
     final taskController = controller.youController ;

     @override Widget build(BuildContext context) { 
        return Scaffold( body: Container(), ); 
      } 
}

try the normal one's

class YourBind extends Binding{

  @override
  void dependencies() {
    // TODO: implement dependencies
     Get.lazyPut(() => TaskController(), fenix: true);
}

Then for the GetPage

class RouteList{

  class RouteList {
  static final routeLists = [
    GetPage(
        transition: Transition.cupertinoDialog,
        transitionDuration: 350.milliseconds,
        binding: YourBind(),
        name: "/home",
        page: () => const SettingPage()),
  ];
}

}

then for the controller

class SettingsController extends GetxController{
  final youController = Get.find<TaskController>();
 
}

next is

class SettingsPage extends GetView<SettingsController> { 
     
     final taskController = controller.youController ;

     @override Widget build(BuildContext context) { 
        return Scaffold( body: Container(), ); 
      } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文