如何编写涉及Flutter Object Box中关系的可订阅查询?

发布于 2025-02-07 11:59:11 字数 1165 浏览 1 评论 0原文

我一直从服务器接收更新并将其放置在我的对象盒数据库中。我的应用程序旨在可视化这些数据。数据涉及跑步者,竞赛课程等。

我要实现的效果是,当对一类跑步者进行更新时,小部件可视化此类(竞赛类 - 不编程课程)知道是时候该请求数据和Redraw了。在我看来,这可能是通过听取按课堂过滤跑步者的查询来完成的。

@Entity()
class Runner {
  int id;
  String name;
    
  final runnerClass = ToOne<Class>();
  // constructors, other fields and methods omitted
}

@Entity()
class Class {
  int id;
  String name;

  @Backlink("runnerClass")
  final runners = ToMany<Runner>();
  // constructors, other fields and methods omitted
}

// in persistence class
late Stream<Query<Runner>> watchedRunners;

// in persistence class, method
QueryBuilder<Runner> query = runnerBox.query();
query.link(Runner_.runnerClass, Class_.name.equals("Easy"));
watchedRunners = query.watch();

// in listening class
final sub = db.watchedRunners.listen((Query<Runner> query) {
  print("THINGS BE HAPPENING");
});

然后,我通过更改服务器上的一个跑步者来对其进行测试。我将他们的班级从不容易的班级更改为另一个不容易的班级。换句话说,轻松班上的跑步者没有获得添加的添加,删除或修改,并且在服务器的XML更新中,我可以看到很多。然而,这个听众开火,我猜是因为跑步者根本有所改变。

如果我在听力课上打印查询结果,它说每个人都在简单的课程中。如果我打印长度,它会不断地说31(而总共有82位跑步者)。如果我实际上将跑步者移入/退出简单的类,则数字更新以反映它,并将命名列表签出。因此,在我看来,查询是正确的,但是听众不是。

I am continuously receiving updates from a server and placing them in my ObjectBox database. My app is meant to visualize this data. The data involves runners, competition classes, etc.

The effect I want to achieve is that when there is an update to a class of runners, the widget visualizing this class (competition class - not programming class) knows it's time to request data and redraw. It seems to me that this might be accomplished by listening to a query that filters runners by class.

@Entity()
class Runner {
  int id;
  String name;
    
  final runnerClass = ToOne<Class>();
  // constructors, other fields and methods omitted
}

@Entity()
class Class {
  int id;
  String name;

  @Backlink("runnerClass")
  final runners = ToMany<Runner>();
  // constructors, other fields and methods omitted
}

// in persistence class
late Stream<Query<Runner>> watchedRunners;

// in persistence class, method
QueryBuilder<Runner> query = runnerBox.query();
query.link(Runner_.runnerClass, Class_.name.equals("Easy"));
watchedRunners = query.watch();

// in listening class
final sub = db.watchedRunners.listen((Query<Runner> query) {
  print("THINGS BE HAPPENING");
});

I then test this by changing one runner on the server. I change their class from one that is not Easy to another that is not Easy. In other words, the runners in the Easy class receive no additions, removals or modifications, and in the XML update from the server, I can see as much. Yet this listener fires, I'm guessing because there was a change to runners at all.

If I print the results of the query in the listening class, it says everyone is in the Easy class; if I print the length it constantly says 31 (whereas there are 82 runners in total). If I actually move runners in/out of the Easy class, the number updates to reflect it, and the list of names checks out. So it seems to me the query is correct, but the listener is not.

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

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

发布评论

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