简体   繁体   English

如何将 Text 变量的初始值提供给 Text Widget Flutter

[英]How to supply an Initial Value of the Text variable into a Text Widget Flutter

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.在用户放置他们自己的图像之前从颤动进入 circleAvatar 我想用初始值替换空格,将 currentUser 变量名称的第一位数字替换为 TextWidget。 it means for example the Name is "john" I want to get "J" how we can reach that?这意味着例如名称是“约翰”我想得到“J”我们如何才能达到那个?

  
  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]通过这种方式我得到了它,但我不知道如果 imageUrl 为空如何返回 currentUser.name[0]

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是否为null来获取带有字符文本小部件作为子项而不是图像的圆形头像:

imageUrl ? CircleAvatar(backgroundImage: NetworkImage(imageUrl),)
 : CircleAvatar(backgroundColor: Colors.brown.shade800,
    child: Text(currentUser && currentUser.isNotEmpty? currentUser[0] : "default"),)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM