代码之家  ›  专栏  ›  技术社区  ›  mducc

snapshot.hasData始终返回false

  •  0
  • mducc  · 技术社区  · 7 年前

    我尝试从api获取数据: https://location-query.herokuapp.com/location?key_word=ha%20noi

    这是响应json:

    {  
       "code":200,
       "key_word":"ha noi",
       "result":[  
          {  
             "id":1581130,
             "name":"Ha Noi",
             "country":"VN",
             "coord":{  
                "lon":105.841171,
                "lat":21.0245
             }
          }
       ]
    }
    

    这是我的数据类:

    class Coord {
      final double lon, lat;
    
      Coord({this.lon, this.lat});
    
      factory Coord.fromJson(Map<String, dynamic> json) {
        return Coord(
          lon: json['lon'],
          lat: json['lat']
        );
      }
    }
    
    class Location {
      final int id;
      final String name, country;
      final Coord coord;
    
      Location({this.id, this.name, this.country, this.coord});
    
      factory Location.fromJson(Map<String, dynamic> json) {
        return Location(
          id: json['id'],
          name: json['name'],
          country: json['country'],
          coord: Coord.fromJson(json['coord'])
        );
      }
    }
    
    class Locations {
      final int code;
      final String key_word;
      final List<Location> result;
    
      Locations({this.code, this.key_word, this.result});
    
      factory Locations.fromJson(Map<String, dynamic> json) {
        return Locations(
            code: json['code'],
            key_word: json['key_word'],
            result: json['result'].map((value) => Location.fromJson(value)).toList()
        );
      }
    }
    

    class SearchLocation {
    
      Future<Locations> search(key_word) async {
        final get = await http.get(Api().searchLocation(key_word));
        if (get.statusCode == 200)
          return Locations.fromJson(json.decode(get.body));
        else
          throw Exception('Can not request');
      }
    
    }
    

    这是代码读取数据,但是 snapshot.hasData

    SearchLocation searchLocation = SearchLocation();
    _searchResult = searchLocation.search(_controller.text);
    
    FutureBuilder<Locations>(
                    future: _searchResult,
                    builder: (context, snapshot) {
                      if (snapshot.hasData) {
                        return Text('Done');
                      }
                      else {
                        print('Location: No Result');
                        return Text('No result');
                      }
                    })
    
    0 回复  |  直到 7 年前
        1
  •  0
  •   Bhoomika Chauhan    7 年前

    请尝试替换以下代码:

    FutureBuilder<Locations>(
            future: search(_controller.text),
            builder: (context, snapshot) {
              if(snapshot.connectionState == ConnectionState.done){
                return Text('Done');
              }else {
                print('Location: No Result');
                return Text('No result');
              }
            });
    

    位置模型类

    class Locations {
      final int code;
      final String key_word;
      final List<Location> result;
    
      Locations({this.code, this.key_word, this.result});
    
      factory Locations.fromJson(Map<String, dynamic> json) {
        return Locations(
            code: json['code'],
            key_word: json['key_word'],
            result: (json['result'] as List).map((value) => Location.fromJson(value)).toList()
        );
      }
    }
    

    如果你想知道更多的信息,一定要告诉我。