I'm using a ChangeNotifierProvider to provide a boolean which returns true or false depending on whether the user is logged in. How do I make the boolean listenable so that provider updates it automatically when the user logs in/out?
Widget build(BuildContext context) {
var socialProvider = Provider.of<SocialProvider>(context);
return Container(
child: new FlatButton(
onPressed: () {
if (
socialProvider.currentlogged != true
) {
Do something
} else {
Do something else
},
),}
Best way to implement authentication functionality using provider is by wrapping your parent class with Provider. For example,
MultiProvider(
providers: [
ChangeNotifierProvider(
create: (_) => Authentication(),
),
)
],
child: MaterialApp(
........
),
In this way you can consume the provider anywhere from the app just by wrapping child widget with Consumer.
Consumer<Authentication>(
builder: (context, auth, _) => Container(
child: new FlatButton(
onPressed: () {
if (auth.currentlogged != true) {
Do something
} else {
Do something else
}
Since authentication controls the entire app itself I wrapped MaterialApp with Provider you can do it in any widget so that all of its children can consume it. When you call notifyListeners(); in a provider, it will re-render the whole consumer part.