rrousselGit / state_notifier

ValueNotifier, but outside Flutter and with some extra perks
MIT License
311 stars 28 forks source link

ProxyProvider for StateNotifier #16

Closed chimon2000 closed 4 years ago

chimon2000 commented 4 years ago

I am currently trying to rebuild my StateNotifierProvider based on some external values, but cannot find a way to achieve this. Basically I have something like the following:

 IssueQuery(
    organization: state.organization,
    initialCount: 25,
    builder: (state, {fetchMore, refetch}) {
      return MultiProvider(
        providers: [
          StateNotifierProvider(
            create: (context) => QueryController(
              state,
              fetchMore: fetchMore,
              refetch: refetch,
            ),
          ),
        ],
        child: MaterialApp(
          theme: ThemeData(
            primarySwatch: Colors.blueGrey,
          ),
          home: HomePage(),
        ),
      );
    },
  );

The state from the builder method should trigger a new state for QueryController but it isn't.

smiLLe commented 4 years ago

You could expose the state, fetchMore and refetch on another StateNotifier and use LocatorMixin in QueryController to handle changes on state

chimon2000 commented 4 years ago

@smiLLe wouldn't that have the same issue of not updating when they are passed into the provider?

smiLLe commented 4 years ago

You don't pass them into the constructer but watch their values through the LocatorMixin in your QueryController

rrousselGit commented 4 years ago

As stated by smiLLe, use LocatorMixin's update method to modify the StateNotifier instead of re-creating it.