flutter - AutoCompleteTextField not Showing json Data Flutter
问题描述
Here i am trying to show all collage name in textfield through json and i dont't know why it not showing me allcollagename List data is showing in my debug console but it is not visible in AutoCompleteTextField
here's the api which i am using to fetch data for collage names
var api = 'http://universities.hipolabs.com/search';
here's my fetch data function.
fetchData() async {
try {
var res = await http.get(api);
var data = jsonDecode(res.body);
List<CollageName> collageNameList = data.map<CollageName>((sup) {
return CollageName.fromJson(sup);
}).toList();
allCollageList = collageNameList;
return allCollageList;
} catch (err) {
return Future.error("Connection Error Restart App");
}
}
i am calling this fetch data function in initState and here's my code for AutoCompleteTextField
Container(
child: AutoCompleteTextField(
key: key,
controller: _suggetionTextFieldController,
suggestions: allCollageList,
clearOnSubmit: false,
style: TextStyle(fontSize: 10),
itemFilter: (item, query) {
return item.name
.toLowerCase()
.startsWith(query.toLowerCase());
},
itemSorter: (a, b) {
return a.name.compareTo(b.name);
},
itemSubmitted: (item) {
_suggetionTextFieldController.text = item.name;
},
itemBuilder: (context, item) {
return Container(
child: Text(item.name),
);
})),
CollageName(model class)
class CollageName {
String country;
String stateProvince;
String name;
List<dynamic> webPages;
List<dynamic> domains;
String alphaTwoCode;
CollageName(
{this.country,
this.stateProvince,
this.name,
this.webPages,
this.domains,
this.alphaTwoCode});
CollageName.fromJson(Map<String, dynamic> json) {
country = json['country'] ?? "ohh bhai tu fir aa gaya";
stateProvince = json['state-province'];
name = json['name'] ?? "ohh bhai tu fir aa gaya";
webPages = json['web_pages'].cast<String>();
domains = json['domains'].cast<String>();
alphaTwoCode = json['alpha_two_code'] ?? "ohh Bhai";
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['country'] = this.country;
data['state-province'] = this.stateProvince;
data['name'] = this.name;
data['web_pages'] = this.webPages;
data['domains'] = this.domains;
data['alpha_two_code'] = this.alphaTwoCode;
return data;
}
}
init State
@override
void initState() {
super.initState();
fetchData();
print(allCollageList.toString());
}
解决方案
fetchData()
对您的方法进行一些更改
fetchData() async {
try {
var res = await http.get(api);
var data = jsonDecode(res.body);
List<CollageName> collageNameList = data.map<CollageName>((sup) {
return CollageName.fromJson(sup);
}).toList();
// added these 3 lines also, notice that I have used .addAll() method instead of using the assignment operator
setState(() {
allCollageList.addAll(collageNameList);
});
} catch (err) {
return Future.error("Connection Error Restart App: " + err);
}
}
推荐阅读
- python - 如何向鼠标旋转精灵并移动它?
- android - Android Studio 4.1版本看不到flutter、dart插件
- mapbox - 如何通过额外 json 文件中的数据更改特征的样式/属性?(地图盒 GL)
- android - Android中带有通知的AlarmManager不显示任何通知
- c# - .NETStandard 多目标库包在 Netstandard 2.0 项目中出现 CS0234 错误
- python - 如何在文档字符串的表格中设置代码块对齐方式?
- svelte - 将数据传递给组件时使用扩展属性与整个对象
- android - 如何使用 kotlin 从 firebase 实时数据库中检索数据
- c++ - c++编译错误使用set [二进制表达式的无效操作数('const Room'和'const Room')]
- php - 无法在 PHP 中访问 OpenSSL eNULL 密码