0

我正在使用来自 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
    });
  }
4

1 回答 1

0

在消息文档上,添加一个名为的字段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(如果定义了模型)

于 2021-09-17T22:54:24.920 回答