How get the first letter of displayName of a user

2020-05-09 17:33发布

问题:

And How capitalize the displayName first element?

How I get the capital first letter of displayName of user, who just registered on my app and store the first letter in firestore.

Here is my code to store the data in firestore.

  void updateUserData(FirebaseUser user) async {
DocumentReference ref = _db.collection('users').document(user.uid);
return ref.setData({
  'uid': user.uid,
  'email': user.email,
  'photoURL': user.photoUrl,
  'displayName': user.displayName,
  'lastSeen': DateTime.now()
}, merge: true);

}

Eg Suppose displayName = richie, here what

I want to store a new key value pair in my document

firstletter = R

Thanks In Advance

回答1:

It looks like you are already storing the display name in your Firestore Database. I would recommend to not store the first letter in the database as well because this seems a bit redundant, but do with it as you will. In Dart you can use .toUpperCase() to turn a string into all Uppercase. give this a try:

String firstLetter = user.displayName.substring(0,1).toUpperCase();