Reputation: 311
Into the circleAvatar from flutter before user put they own image I want to replace the space with the Initial value the first digit of the currentUser variable name into TextWidget. it means for example the Name is "john" I want to get "J" how we can reach that?
final imageUrl = true;
and build method
imageUrl ? CircleAvatar(
radius: 60,
backgroundImage: AssetImage(currentUser.imageUrl),)
: CircleAvatar(backgroundColor: Colors.yellow,
child: Text(currentUser.name[0]),),
SizedBox(
height: 60,
),
In this way I get it but I dont know how to if imageUrl is null return currentUser.name[0]
Upvotes: 0
Views: 859
Reputation: 1385
to get the first character of a name:
final s = "John";
print(s[0]); // => J
to guard against null, you can use the ??
or the ?
operator.
to get a circle avatar with a character text widget as child instead of an image, depending on whether imageUrl
is null
or not:
imageUrl ? CircleAvatar(backgroundImage: NetworkImage(imageUrl),)
: CircleAvatar(backgroundColor: Colors.brown.shade800,
child: Text(currentUser && currentUser.isNotEmpty? currentUser[0] : "default"),)
Upvotes: 1