【问题标题】:snapshot.hasData alway return falsesnapshot.hasData 总是返回 false
【发布时间】:2019-05-06 04:31:55
【问题描述】:

我尝试从 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');
                  }
                })

【问题讨论】:

  • 你确定没有抛出异常吗? snapshot.hasError 的值是多少?
  • 我有一个关于 snapshot.hasError 的异常,但我不知道如何显示日志错误
  • 你检查了官方文档中的所有AsyncSnapshot属性吗?
  • 现在就阅读吧……

标签: android ios dart flutter


【解决方案1】:

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

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

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

【讨论】:

  • @mducc 如果这个答案对你有用,请告诉我。您还可以通过支持我的回答来帮助我进入这个社区。高级感谢。
  • 我修好了,这不是你的方式
猜你喜欢
  • 2021-09-10
  • 2021-06-11
  • 1970-01-01
  • 1970-01-01
  • 2016-07-20
  • 2021-11-05
  • 2018-11-05
  • 2019-05-06
  • 2017-04-29
相关资源
最近更新 更多