如何根据条件配置颤振中的文本值



我从服务器获取以下单字符代码,如flutter应用中的'M'、'T'或'B'等

M = Manual
T = Trial
B = Baking
N = N/A
S = Source
O = Other Source

现在,在列表视图中只配置了Text()元素,该元素必须根据检索到的代码来表示"Manual"、"Trial"或"Baking"等值。

如果只有两个元素可供选择,我知道如何实现三元运算符。但在这种情况下,有6种选择,只有一种是代表。

我应该如何在flutter listview生成器中配置它??

child:Text('value to be represented here')

我不明白"if else if-else"在这里是如何工作的?

使用映射。

void main() {
Map<String, String> values = {
"M": "Manual",
"T": "Trial",
"B": "Baking",
"N": "N/A",
"S": "Source",
"O": "Other Source",
};
print(values["M"]); // Manual
print(values["O"]); // Other Source
}

编写如下函数:

// The code represents the single character code
String getNameFromCode(String code){
switch(code){
case 'M': return 'Manual';
case 'T': return 'Trial';
case 'B': return 'Baking';
case 'N': return 'N/A';
case 'S': return 'Source';
case 'O': return 'Other Source';
}
}

使用该功能作为文本窗口小部件的输入:

Text(getNameFromCode(getDataFromServer()))

我假设您需要输出一个值;我不知道你为什么需要ListView

有关此类代码的列表:

List<String> codes = getDataFromServer()
ListView.builder(
itemCount:
itemBuilder: (ctx, i){
return Text(getNameFromCode(codes[i]));
}
)

相关内容

最新更新