我是 Flutter 的新手。我想根據用戶輸入的數字前綴更改字串“operatorIcon”的影像,但我下面的代碼不起作用。影像仍然沒有改變
這是我的模特
class PrefixOperator {
final String id;
final String name;
final String image;
final List<PrefixList> prefixList;
PrefixOperator({
required this.id,
required this.name,
required this.image,
required this.prefixList,
});
}
class PrefixList {
final String id;
final String code;
PrefixList({
required this.id,
required this.code,
});
}
List<PrefixOperator> prefixOperator = [
PrefixOperator(
id: "1",
name: "aaa",
image: AssetsCollection.icAaa,
prefixList: prefixAaa,
),
PrefixOperator(
id: "2",
name: "bbb",
image: AssetsCollection.icBbb,
prefixList: prefixBbb,
),
];
List<PrefixList> prefixAaa = [
PrefixList(id: "1", code: "0711"),
PrefixList(id: "2", code: "0712"),
PrefixList(id: "3", code: "0713"),
];
List<PrefixList> prefixBbb = [
PrefixList(id: "1", code: "0614"),
PrefixList(id: "2", code: "0615"),
PrefixList(id: "3", code: "0616"),
];
這是我的有狀態小部件
final TextEditingController _controller = TextEditingController();
String phoneNumber = "";
bool isContains = false;
String prefixNumber = "";
String operatorIcon = "";
handleTextChange() {
if (_controller.text != "" && _controller.text.length > 8) {
setState(() {
prefixNumber = _controller.text.substring(0, 4);
});
}
}
checkIcon() {
if (prefixNumber != "") {
prefixOperator.map((item) {
isContains =
item.prefixList.map((e) => e.code).toList().contains(prefixNumber);
if (isContains == true) {
setState(() {
operatorIcon = item.name;
});
}
});
}
}
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Row(children: [
Image.asset(operatorIcon != "" ? operatorIcon : widget.icon,
height: 40),
Expanded(
child: Column(
children: [
SizedBox(
height: 10,
),
InputLabel(
labelText: "Phone Number",
isRequired: false,
),
TextField(
controller: _controller,
onChanged: (value) {
handleTextChange();
checkIcon();
},
keyboardType: TextInputType.phone,
decoration: InputDecoration(
hintText: 'Enter your mobile number',
),
),
],
),
)
]),
],
);
}
..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................
uj5u.com熱心網友回復:
你知道這map不是一個void函式嗎?
Iterable<T> map<T>(T toElement(E e)) sync* {
for (var value in this) {
yield toElement(value);
}
}
map is 回傳一個新的惰性 Iterable,其中包含通過按迭代順序在此 Iterable 的每個元素上呼叫 toElement 創建的元素。
所以,你錯過了什么。只需.toList()在您的第一張地圖上做廣告。你會得到價值。
prefixOperator.map((item) {
isContains =
item.prefixList.map((e) => e.code).toList().contains(prefixNumber);
....
}).toList(); // add this
我在 dartpad 上測驗過。它作業正常
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/520972.html
標籤:扑镖
