首页 > 解决方案 > 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());
  }

标签: flutterdartflutter-layoutflutter-dependencies

解决方案


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);
    }
  }

推荐阅读