Skip to content

Feature request: ref.listen or select should support filtering null values #1347

@MarkusKnochKenbun

Description

@MarkusKnochKenbun

Feature request: ref.listen should support filtering null values

It's often the case for me that I use a AsyncNotifier and I need to run side effect in a listen and most of the time I combine it with selects, but they are always awkward to handle. I want to only fire the side effect when there is actually data present.

Currently the workaround is an early return null guard inside the callback:

ref.listen(
  myProvider.select((s) {
    final value = s.value;
    if (value == null) return null;
    return (foo: value.foo, bar: value.bar);
  }),
  (previous, next) {
    if (previous == null || next == null) return; // awkward
    // actual logic
  },
);

It would be nicer to have a way to express this more cleanly, for example a listenWhenData variant or a selectWhenData.

ref.listenWhenData(
  myProvider,
  (value) => (foo: value.foo, bar: value.bar),
  (previous, next) {
    // previous and next are guaranteed non-null here
  },
);
ref.listen(
  myProvider.selectWhenData((value) => (foo: value.foo, bar: value.bar)),
  (previous, next) {
    // previous and next are guaranteed non-null here
  },
);

Selecting on non async Providers is always so easy but for AsyncProviders it feels very unintuitive for now.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions