River_pod: Add an option in Consumer when to build, when using StateNotifier

Created on 7 Oct 2020  Â·  9Comments  Â·  Source: rrousselGit/river_pod

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Inside a consumer I have a widget that i don't want to rebuild when certain condition is met.

Describe the solution you'd like
A clear and concise description of what you want to happen.

I would suggest if Consumer will also have buildWhen like flutter_bloc.

Consumer(
  buildWhen: (prevState, currentState) {
  // check condition
  },
  builder: (context, watch, child) {
  // ...
};

I don't know if StateNotifier track the previous state. This is just a suggestion. : - )

enhancement

Most helpful comment

There are currently two ways:

  • useProvider(myProvider.select(...))
  • extracting the value you want to listen in a separate Provider
final todoList = StateNotifierProvider(...)

final todosCount = Provider((ref) {
  return ref.watch(todoList.state).length;
});

...

final count = watch(todosCount)

All 9 comments

There is no thing such as previous/next state with Consumer

The syntax will instead be:

Consumer(
  builder: (context, watch, child) {
    final name = watch(myProvider.select((model) => model.name));
  } 
)

There is no thing such as previous/next state with Consumer

The syntax will instead be:

Consumer(
  builder: (context, watch, child) {
    final name = watch(myProvider.select((model) => model.name));
  } 
)

So there is no way no compare the previous and current state? Anyway thanks! I'll try this syntax.

Hi @rrousselGit,

How do I select a field from a StateNotifier? I tried the code below but it shows a warning like 'The member 'state' can only be used within instance members of subclasses of 'package:state_notifier/state_notifier.dart''.

Consumer(
  builder: (context, watch, child) {
    final name = watch(myStateNotifierProvider.select((myStateNotifierProvider) => myStateNotifierProvider.state.name));
  } 
);

This is not yet implemented

On Sun, Oct 11, 2020, 09:17 Gerald notifications@github.com wrote:

Hi @rrousselGit https://github.com/rrousselGit,

How do I select a field from a StateNotifier? I tried the code below but
it shows a warning like 'The member 'state' can only be used within
instance members of subclasses of
'package:state_notifier/state_notifier.dart''.

Consumer(
builder: (context, watch, child) {
final name = watch(myStateNotifierProvider.select((myStateNotifierProvider) => myStateNotifierProvider.state.name));
}
);

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/rrousselGit/river_pod/issues/176#issuecomment-706668673,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AEZ3I3KCU46JAWX3FRJJO3DSKFS2HANCNFSM4SHSQD5A
.

I think alternatively it works if you combine AsyncValue with StateNotifier. Then inside Consumer.

 final user = watch(userProvider).whenData((user) => user.name);
});

Inside providers it works.

@iamarnas ohhh. thanks! I'll try it.

when/whenData does not filter rebuilds.

when/whenData does not filter rebuilds.

Is the only way to filter rebuilds is with the Hooks selector?

There are currently two ways:

  • useProvider(myProvider.select(...))
  • extracting the value you want to listen in a separate Provider
final todoList = StateNotifierProvider(...)

final todosCount = Provider((ref) {
  return ref.watch(todoList.state).length;
});

...

final count = watch(todosCount)
Was this page helpful?
0 / 5 - 0 ratings