I have tired to stream user authentication state changes using StreamProvider.value to MaterialApp. But i got this Error: A value of type 'MultiProvider' can't be returned from method 'build' because it has a return type of 'Widget'.
This are my the dependency in pubspec.yaml
firebase_core: ^0.5.0 firebase_auth: ^0.18.0+1 cloud_firestore: ^0.14.0+2 provider: ^4.3.2+2
And couldn't be able to figure it out. I'm stuck please help. Thank you for your help in advance.
import 'package:provider/provider.dart';
import 'package:flutter/material.dart';
class MyAwesomeApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [StreamProvider<Usr>.value(value: Authenticate().userStream)],
builder: (context, child) {
return MaterialApp(
home: Wrapper(),
);
},
);
}
}
//This is Authenticate class which holds the stream
import 'package:firebase_auth/firebase_auth.dart';
class Authenticate {
//create an instance of firebase auth
FirebaseAuth _auth = FirebaseAuth.instance;
// Create a new user instance from my user model
Usr _userFromFirebaseUser(User user) {
return user != null ? Usr(uid: user.uid) : null;
}
// set up auth stream to listen to user auth status
Stream<Usr> get userStream {
return _auth.authStateChanges().map(
_userFromFirebaseUser); // shorter way of mapping to user custom user object
// .map((User user) => _userFromFirebaseUser(user)); //longer one
}
// sign in anon
Future signInAnon() async {
try {
UserCredential response = await _auth.signInAnonymously();
User user = response.user;
return _userFromFirebaseUser(user);
} catch (e) {
print(e.toString());
return null;
}
}
}