mobxjs / mobx.dart

MobX for the Dart language. Hassle-free, reactive state-management for your Dart and Flutter apps.
https://mobx.netlify.app
MIT License
2.39k stars 311 forks source link

Make the Observer to rebuild when no changes in immediate context #954

Closed subzero911 closed 10 months ago

subzero911 commented 10 months ago

I have a Text widget which should rebuild. But Observables lie in the nested getDateText() function:

image image

This function formats observables with DateFormat before printing it.

The only way I've come up with is to perform a fake reading to make the Observer rebuild:

image

Is there a more correct way in MobX to solve such an issue? Maybe there's some way to manually trigger the Observer?

subzero911 commented 10 months ago

I got it, I should have just extract observable values in the Observer body and pass it to the function as parameters:

Observer(builder: (_) {
  DateTime? verifyAt = task.verifyAt.value;
  DateTime? doneAt = task.doneAt.value;
  TaskStatus status = task.status.value;
  DateTime? deadline = task.endAt;
  return Text(
    presenter.getDateText(verifyAt, doneAt, status, deadline),
  );
});

Here's a problem though, there's a lot of code in the UI layer. It's impossible to move it outside of the Observer body as it won't react then.

subzero911 commented 10 months ago
image

I understood. I should do a computation not in the parameter, but in the Observer body.