颤振类型 'Rx'不是“DateTime”类型的子类型;在类型转换中

发布于 2025-01-12 12:44:51 字数 494 浏览 1 评论 0原文

我想在 getx 中使用 DateTime 变量 并使用 otherPage 中的 selectedDate 作为 Condition 中的变量 我的控制器代码:

class HomeController extends GetxController {
  RxBool getMyPerm = false.obs;
  Rx<DateTime> selectedDate = DateTime.now().obs;
  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    selectedDate =
        DateTime(selectedDate.year, selectedDate.month, selectedDate.day);
  }
}

但是 flutter 显示了这个错误: 类型Rx<日期时间>不是类型转换中“DateTime”类型的子类型

I want to use a DateTime variable in getx
and use selectedDate in otherPage as variable in Condition
my Controller code :

class HomeController extends GetxController {
  RxBool getMyPerm = false.obs;
  Rx<DateTime> selectedDate = DateTime.now().obs;
  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    selectedDate =
        DateTime(selectedDate.year, selectedDate.month, selectedDate.day);
  }
}

but flutter show me this error :
type Rx< DateTime> is not a subtype of type 'DateTime' in type cast

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

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

发布评论

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

评论(1

不打扰别人 2025-01-19 12:44:51

您无法将 DateTime 分配给 Rx,这是导致问题的行:

 selectedDate =  DateTime(selectedDate.year, selectedDate.month, selectedDate.day);

为了分配 RX 变量的值,您必须通过 value 属性来执行此操作,如下所示:

 selectedDate.value =
    DateTime(selectedDate.year, selectedDate.month, selectedDate.day);

You can't assign a DateTime to an Rx, this is the line that's causing the issue :

 selectedDate =  DateTime(selectedDate.year, selectedDate.month, selectedDate.day);

in order to assign the value of the RX variable you have to do it through the value property like the following :

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