I was following TheNetNinja's tutorial on YouTube : (https://www.youtube.com/watch?v=v3sY3RWciNw&list=PL4cUxeGkcC9j--TKIdkb3ISfRbJeJYQwC&index=9), When I try to sign out, It gives the exception:
An exception was throw by _MapStream<User?, UserObject?> listened by StreamProvider<UserObject?>, but no
catchErrorwas provided.
I've read all the solutions on the net, but couldn't find one that fits. Since the video is from Oct 2019, I had to upgrade some parts of the code. I'll be very grateful If someone helps...
Here's AuthService class:
final FirebaseAuth _auth = FirebaseAuth.instance;
//create user obj based on Firebase user
UserObject? _userObjectFromFirebaseUser(User user) {
return user != null ? UserObject(uid: user.uid) : null;
}
//auth change user stream
Stream<UserObject?> get user {
return _auth
.authStateChanges()
.map((User? user) => _userObjectFromFirebaseUser(user!));
}
//sign in anon
Future signInAnon() async {
try {
UserCredential result = await _auth.signInAnonymously();
User? user = result.user;
return _userObjectFromFirebaseUser(user!);
} catch (e) {
print(e.toString());
return null;
}
}
//sign in with email and password
//register with email and password
//sign out
Future signOut() async {
try {
return await _auth.signOut();
} catch (e) {
print(e.toString());
return null;
}
}
}
User object class:
class UserObject {
final String uid;
UserObject({required this.uid});
}
main:
class MyApp extends StatelessWidget {
final Future<FirebaseApp> _fbapp = Firebase.initializeApp();
MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return StreamProvider<UserObject?>.value(
initialData: null,
value: AuthService().user,
child: MaterialApp(
home: Wrapper(),
));
}
}
Wrapper:
class Wrapper extends StatelessWidget {
const Wrapper({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final user = Provider.of<UserObject?>(context);
if (user == null) {
return Authenticate();
} else {
return HomeScreen();
}
}
}