问题
In flutter, I'm trying to access Custom Claims to decide which Widget I will show, I was able to get the user as Stream and convert it to a Class doing:
Stream<User> get user {
return _auth.onAuthStateChanged
.map(_userFromFirebaseUser);
}
User _userFromFirebaseUser(FirebaseUser user) {
return user != null ? User(uid: user.uid) : null;
}
I would like to add the user.getIdToken() claims['role'] to the User object (i.e.: User(uid: user.uid, role: token.claims['role']), but I'm not sure how to merge a Future into a Stream resulting in a Stream.
回答1:
There is an "operator" for this problem. It is called asyncMap. It almost has the same functionality as map
except you can pass an async function as the argument.
Stream<User> get user {
return _auth.onAuthStateChanged.asyncMap(_userFromFirebaseUser);
}
Future<User> _userFromFirebaseUser(FirebaseUser user) async {
final tokenResult = await user.getIdToken();
return user != null ? User(uid: user.uid, role: tokenResult.claims['role']) : null;
}
来源:https://stackoverflow.com/questions/60742877/in-flutter-how-can-i-merge-firebase-onauthstatechanged-with-user-gettokenid