android - snapshot.hasData 总是返回 false

标签 android ios dart flutter

我尝试从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 总是返回 false:

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

最佳答案

您可以尝试替换以下代码吗:

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

如果您想了解更多信息,请告诉我。

关于android - snapshot.hasData 总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55998834/

相关文章:

javascript - 如何在 Dart 应用程序中应用 Redux 概念?

objective-c - 在 UIView 层上找不到 '-setBorderColor:' 方法?

ios - 我需要提交以供 Facebook 应用审查吗?

flutter - BlocProvider.value 是多余的吗?

flutter - 在ListWheelScrollView中获取子项的值

ios - UILabel 的 TextAlignment

java - 尝试在 GCM 模式下解密消息时出现 AEADBadTagException

Android Studio 添加图像 Assets 不起作用

android - 如何在抽屉导航的右上角添加十字按钮

java - 来自 Java 的 Firebase 云消息传递无法将通知发送到其他设备