Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Does StateNotifier works with StreamProvider #29

Closed
hsul4n opened this issue Jul 15, 2020 · 6 comments
Closed

[Question] Does StateNotifier works with StreamProvider #29

hsul4n opened this issue Jul 15, 2020 · 6 comments

Comments

@hsul4n
Copy link

hsul4n commented Jul 15, 2020

Does state_notifier support StreamProvider for watching changes so, for example, I'm trying to listen to connectivity changes.

Here's the example:

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        StreamProvider<ConnectivityResult>(
          create: (_) => Connectivity().onConnectivityChanged,
        ),
        ChangeNotifierProvider<Counter>(
          create: (_) => Counter(),
        ),
      ],
      child: MaterialApp(
        home: MyHomePage(),
      ),
    );
  }
}

class Counter extends ValueNotifier<int> with LocatorMixin {
  Counter() : super(0);

  void increment() {
    this.value++;
    if (read<ConnectivityResult>() != ConnectivityResult.none) {
      // call api
    }
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('My Home Page'),
      ),
      body: Center(
        child:
            Text('You clicked button ${context.watch<Counter>().value} times'),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () => context.read<Counter>().increment(),
      ),
    );
  }
}
@hsul4n hsul4n changed the title [Question] Does state_notifier works with StreamProvider [Question] Does StateNotifier works with StreamProvider Jul 15, 2020
@rrousselGit
Copy link
Owner

You can use the update method

ConnectivityResult lastConnectivity;

@override
void update(Reader watch) {
  final connectivity = watch<ConnectivityResult>();
  if (connectivity != lastConnectivity) {
    lastConnectivity = connectivity;
   // TODO API call
  }
}

@hsul4n
Copy link
Author

hsul4n commented Jul 15, 2020

You can use the update method

ConnectivityResult lastConnectivity;

@override
void update(Reader watch) {
  final connectivity = watch<ConnectivityResult>();
  if (connectivity != lastConnectivity) {
    lastConnectivity = connectivity;
   // TODO API call
  }
}

This works great with StateNotifier & StateNotifierProvider but what about if I'm using something like ChangeNotifierProvider or ValueNotifier as my example above.

@rrousselGit
Copy link
Owner

LocatorMixin works only with StateNotifier

@hsul4n
Copy link
Author

hsul4n commented Jul 16, 2020

LocatorMixin works only with StateNotifier

So, is there any future planning for implementing with ‘Provider’.

@rrousselGit
Copy link
Owner

Not for now, no. But PRs are welcomed

@hsul4n
Copy link
Author

hsul4n commented Jul 16, 2020

Not for now, no. But PRs are welcomed

I like that.. 👍

@hsul4n hsul4n closed this as completed Jul 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants