构建 Obx 时抛出以下 TypeErrorImpl,意外的 null 值

发布于 2025-01-15 08:21:23 字数 3341 浏览 0 评论 0原文

当我将 flutter 应用程序作为 Web 应用程序打开时,我目前正在调查以下错误:

在此处输入图像描述

The following TypeErrorImpl was thrown building Obx(has builder, dirty, state: _ObxState#4990d):
Unexpected null value.

The relevant error-causing widget was: 
  Obx Obx:file:///D:/development/flutter/demoapp/lib/root.dart:12:12
When the exception was thrown, this was the stack: 
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 251:49      throw_
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 528:63  nullCheck
packages/demoapp/controllers/authController.dart 13:44                                                                     get user
packages/demoapp/root.dart 14:15                                                                                           <fn>
packages/get/get_state_manager/src/rx_flutter/rx_obx_widget.dart 70:28                                                         build
packages/get/get_rx/src/rx_types/rx_core/rx_interface.dart 26:27                                                               notifyChildren
packages/get/get_state_manager/src/rx_flutter/rx_obx_widget.dart 54:19                                                         build

我的根类:


class Root extends GetWidget<AuthController> {
  @override
  Widget build(BuildContext context) {
    return Obx(() {
      return (Get.find<AuthController>().user != null ? LoginPage() : DashboardPage());
    });
  }
}

我的主类:

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      // initialBinding: AuthBinding(),
        initialRoute: RootRoute,
        opaqueRoute: true,
        unknownRoute: GetPage(name: "/not-found", page: () => PageNotFound(), transition: Transition.noTransition),
        defaultTransition: Transition.native,
        // transitionDuration: Duration.zero,
        getPages: [
          GetPage(name: RootRoute, page: () => Root()),
          GetPage(name: AuthenticationPageRoute, page: () => LoginPage()),
        ],
        debugShowCheckedModeBanner: false,
        title: 'DemoAPP',
        theme: ThemeData(
            hoverColor: Colors.transparent,
            splashColor: Colors.transparent,
            // highlightColor: Colors.transparent,
            primarySwatch: Colors.blue,
            scaffoldBackgroundColor: colorLight,
            // scaffoldBackgroundColor: Colors.white,
            textTheme: GoogleFonts.mulishTextTheme(
                Theme.of(context).textTheme).apply(
                bodyColor: Colors.black
            ),
            pageTransitionsTheme: PageTransitionsTheme(builders: {
              TargetPlatform.iOS: FadeUpwardsPageTransitionsBuilder(),
              TargetPlatform.android: FadeUpwardsPageTransitionsBuilder()
            })
        )
    );
  }
}

有人可以帮助我吗?

当用户登录时不会出现任何问题,用户将直接重定向到 LoginPage Dashboard 页面。另外通过 url /login 一切都很完美。

提前致谢。

I currently investigate the following error, when I open the flutter app as web application:

enter image description here

The following TypeErrorImpl was thrown building Obx(has builder, dirty, state: _ObxState#4990d):
Unexpected null value.

The relevant error-causing widget was: 
  Obx Obx:file:///D:/development/flutter/demoapp/lib/root.dart:12:12
When the exception was thrown, this was the stack: 
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 251:49      throw_
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 528:63  nullCheck
packages/demoapp/controllers/authController.dart 13:44                                                                     get user
packages/demoapp/root.dart 14:15                                                                                           <fn>
packages/get/get_state_manager/src/rx_flutter/rx_obx_widget.dart 70:28                                                         build
packages/get/get_rx/src/rx_types/rx_core/rx_interface.dart 26:27                                                               notifyChildren
packages/get/get_state_manager/src/rx_flutter/rx_obx_widget.dart 54:19                                                         build

My Root class:


class Root extends GetWidget<AuthController> {
  @override
  Widget build(BuildContext context) {
    return Obx(() {
      return (Get.find<AuthController>().user != null ? LoginPage() : DashboardPage());
    });
  }
}

My Main class:

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      // initialBinding: AuthBinding(),
        initialRoute: RootRoute,
        opaqueRoute: true,
        unknownRoute: GetPage(name: "/not-found", page: () => PageNotFound(), transition: Transition.noTransition),
        defaultTransition: Transition.native,
        // transitionDuration: Duration.zero,
        getPages: [
          GetPage(name: RootRoute, page: () => Root()),
          GetPage(name: AuthenticationPageRoute, page: () => LoginPage()),
        ],
        debugShowCheckedModeBanner: false,
        title: 'DemoAPP',
        theme: ThemeData(
            hoverColor: Colors.transparent,
            splashColor: Colors.transparent,
            // highlightColor: Colors.transparent,
            primarySwatch: Colors.blue,
            scaffoldBackgroundColor: colorLight,
            // scaffoldBackgroundColor: Colors.white,
            textTheme: GoogleFonts.mulishTextTheme(
                Theme.of(context).textTheme).apply(
                bodyColor: Colors.black
            ),
            pageTransitionsTheme: PageTransitionsTheme(builders: {
              TargetPlatform.iOS: FadeUpwardsPageTransitionsBuilder(),
              TargetPlatform.android: FadeUpwardsPageTransitionsBuilder()
            })
        )
    );
  }
}

Can someone help me?

When the user is logged in no problem occurs and the user will be redirected to the LoginPage Dashboard page directly. Also via the url /login everything works perfect.

Thanks in advance.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文