我正在嘗試過濾國家串列,但我從 IDE 中收到型別錯誤。
這是錯誤訊息:
“可迭代”型別的值不能分配給“串列”型別的變數。嘗試更改變數的型別,或將右側型別轉換為“串列”。
這是我的小部件:
const List<String> countriesList = <String>["Aphganistan",...];
//This countriesList comes from constants/countries.dart
class SignUp extends StatefulWidget {
const SignUp({super.key});
@override
State<SignUp> createState() => _SignUpState();
}
class _SignUpState extends State<SignUp> {
static List<String> _allCountries = countriesList;
static List _foundCountries = [];
@override
void initState() {
_foundCountries = _allCountries;
super.initState();
}
@override
Widget build(BuildContext context) {
return Container(
child: TextFormField(
onChanged: (String enteredKeyword) {
List<dynamic> results = [];
if (enteredKeyword.isEmpty) {
results = _allCountries;
} else {
results = _allCountries.where((element) => false); // Here i get this error!
}
setState(() {
_foundCountries = results;
});
log(enteredKeyword);
},
),
);
}
}
我試圖將 List 更改為 Iterable,但是當我嘗試獲取 ListView.builder 中的元素時出現錯誤 => _foundCountries[index] // 它回傳錯誤。
uj5u.com熱心網友回復:
您需要將可迭代物件更改為串列,如下所示:
results = _allCountries.where((element) => false).toList();//<- change
哪個回傳資料串列...
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/535427.html
標籤:扑