使用 getx 控制器时,未为类型“Rx<某个类>”定义 getter“某个变量”

大PP

我正在尝试为我的项目实施反应式 getx以实现更清晰的代码。

在控制器飞镖文件中,我有:

class ReadSinglePostController extends GetxController {
  var posts = Post(
          postID: 1,
          userID: 0,
          thumbnail: 'some base64string',
          imageList: 'some base64string',
          title: 'title',
          description: 'description',
          createdTime: DateTime.now())
      .obs;//initialization and make the class Post observable
  var postid = 0.obs; //initialize postid and make it observable
  void updateID(var postID) {
    postid = postID;
  } //update the postid variable when a specific post is clicked in the post list

  @override
  void onInit() {
    super.onInit();
    readPost(postid);
  }

  Future readPost(var postID) async {
    var result = await PostsDatabase.instance.readNote(postID);
    posts = result;
  }//function to read the detailed content of that specific post from database passing in the postid
}

然后在 UI 的主页上,我将所有帖子都放在一个网格中,我实现了这个控制器以在postid单击特定帖子时更新单击帖子时,它会导航到帖子详细信息页面,并在该 UI 文件中出现错误

Error:The getter "thumbnail" isn't defined for the type 'Rx<Post>'. 当我使用 getter 'title'、'description' 等时,会出现同样的错误。

这是代码:

class PostBody extends StatelessWidget {
  PostBody({
    Key? key,
  }) : super(key: key);

  final readSinglePostController = Get.put(ReadSinglePostController());

  @override
  Widget build(BuildContext context) {
    return DefaultTextStyle(
      style: Theme.of(context).textTheme.bodyText2!,
      child: LayoutBuilder(
        builder: (BuildContext context, BoxConstraints viewportConstraints) {
          return SingleChildScrollView(
            child: ConstrainedBox(
              constraints: BoxConstraints(
                minHeight: viewportConstraints.maxHeight,
              ),
              child: GetX<ReadSinglePostController>(
                builder: (controller) {
                  return ...
                              ...
                                ...
                                child: FadeInImage(
                                  placeholder: MemoryImage(kTransparentImage),
                                  image:
                                      Utility.imageFromBase64String(
                                              controller.posts.thumbnail)//Error:The getter "thumbnail" isn't defined for the type 'Rx<Post>'. and the same error comes up when I use getter 'title', 'description' and etc.
                                          .image,
                                ),...

有什么线索吗,伙计们?

尼桑斯·雷迪

IN Getx,Rx<T>T.

这意味着当您调用时,controller.posts您将获得一个响应式类型的实例,即Rx<Post>.

现在,Rx<Post>不会直接知道内部存在哪些成员Post,这就是您无法直接访问它们的原因。

相反,您必须首先value从反应式对象中获取 ,它为您提供了包装的实际Post实例Rx从中您可以访问您的实例变量。

controller.posts.value.thumbnail

这里,controller.posts.value是 类型Post

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何定义某个类的类型保护方法?

使用jQuery达到某个高度时更改div类?

在Flutter GetX中使用没有控制器的可观察变量

是否可以在某个部分使用 Getx 状态管理,但不能在 Flutter App 的其他部分使用?

当参数为某个值时定义返回类型

仅当字段是某个枚举变量时才为结构定义方法吗?

在类及其某个祖先之间定义的实例方法

如何声明扩展某个类的类型字段?

为我的某个视图控制器使用扩展

Fluter:如何使用Getx更改变量?

使用对先前标记的引用以及与某个类的子类型相对应的子代来解析XML

如何使用 getx 在颤振中的一个控制器中获取另一个控制器的变量

在Bootstrap设置中将鼠标悬停时使用jQuery删除某个类

在 Django 中,是否可以为某个超类定义外键,但在查询时返回了子类?

使用MouseClicked使用光标的坐标获取此错误。“对于MouseEvent类型,未定义方法getX()和getY()”

使用 getX 抖动图像选择器

Checkstyle检查某个类仅在某个包或子包中使用

如何定义参数的类型提示(参数的值是一个类,所有期望值都是某个类的子类)?

当继承的类中的变量等于某个值时,如何运行方法?

WORDPRESS:使用自定义帖子类型显示自定义分类法中某个类别的帖子

如何使用类访问数组中的某个索引?

如何使用 puppeteer 检查 div 是否具有某个类?

使用CSS减少某个类的任何元素的宽度和高度

如何使用 javascript 给某个 td 一个类名?

如果服务器响应在 Angular 2 中未定义,则将变量设置为某个值

如何根据某个变量定义一个实例?

使用接口或类定义变量

如何使swift类符合某个协议,而该协议的属性之一要求使用符合协议的类型?

从静态类返回某个类型的常量字段值数组