我如何让我的应用程序显示多个用户名?



我正在使用firebase auth的电子邮件和密码登录。

现在当我更改我的帐户时,显示的用户名将根据更改的帐户而更改。我想在不更改用户名的情况下发布多个帐户。

这是用来声明名称的代码:

Widget displayUserInformation(context, snapshot) {
final user = snapshot.data;
return Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
user.displayName,
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
),
],
);
}

注册码:

// Email & Password Sign Up
Future<String> createUserWithEmailAndPassword(
String email, String password, String name) async {
final authResult = await _auth.createUserWithEmailAndPassword(
email: email,
password: password,
);
// Update the username
await updateUserName(name, authResult.user);
return authResult.user.uid;
}

邮政编码:

static Future<void> sendPostInFirebase(String postID, String postContent,
MyProfileData displayName, String postImageURL) async {
String postFCMToken;
if (displayName.myFCMToken == null) {
SharedPreferences prefs = await SharedPreferences.getInstance();
postFCMToken = prefs.get('FCMToken');
} else {
postFCMToken = displayName.myFCMToken;
}
Firestore.instance.collection('thread').document(postID).setData({
'postID': postID,
'displayName': displayName.email,
'userThumbnail': displayName.myThumbnail,
'postTimeStamp': DateTime.now().millisecondsSinceEpoch,
'postContent': postContent,
'postImage': postImageURL,
'postLikeCount': 0,
'postCommentCount': 0,
'FCMToken': postFCMToken
});
}

在消息文档中,添加一个名为username的字段,其中包含发布消息的用户名。然后在需要的地方显示该用户名。

// ... other code here
Firestore.instance.collection('thread').document(postID).setData({
'postID': postID,
'displayName': displayName.email,
'userThumbnail': displayName.myThumbnail,
'postTimeStamp': DateTime.now().millisecondsSinceEpoch,
'postContent': postContent,
'postImage': postImageURL,
'postLikeCount': 0,
'postCommentCount': 0,
'FCMToken': postFCMToken,
// add username of poster,
'username': 'PUT THE USERNAME HERE',
// e.g 'username': FirebaseAuth.instance.currentUser.displayName,
});

然后当你收到消息时,用户名随它而来,你可以像这样访问它:message['username']message.username(如果模型被定义)

相关内容

最新更新