I’m learning Dart
and I understand that the below (should be) correct. I now want to understand how to do the shorthand version of this. This function lives in my state container (inherited widget) so that I can call this anywhere in my app. Hopefully calling this function everywhere in my app doesn’t count as multiple Firestore queries…
String getUID() {
final uid = FirebaseAuth.instance.currentUser?.uid.toString();
if (uid == null || uid == '') {
return '';
} else {
return uid;
}
}
3
Answers
how about make on
helper.dart
then you can call
getUID
in every classim not sure this one is best solution, but this what i usually did for common function.
You could write it like this:
or also as a getter (the getter won’t need parentheses to be accessed (), you just call it as if it were an variable .uid):
The ?? operator will work as:
You don’t need to check for empty string ” there because you are passing it as empty anyway.
You can write it like so
There is no need for
toString()
asuid
itself is already aString
.