【问题标题】:_TypeError in call api flutter_TypeError in call api flutter
【发布时间】:2020-11-28 07:41:09
【问题描述】:

下午好,我正在调用我的 api 来带上我注册的企业。

这是我的模特

class Shops{
    Shops({
        this.id,
        this.name,
        this.phone,
        this.merchantType,
        this.address,
        this.city,
        this.distance,
        this.logo,
    });

    String id;
    String name;
    String phone;
    String merchantType;
    String address;
    String city;
    double distance;
    String logo;

    factory Shops.fromJson(Map<String, dynamic> json){
      return Shops(
        
        id: json["_id"],
        name                    : json["name"],
        phone                   : json["phone"],
        merchantType            : json["merchantType"],
        address                 : json["address"],
        city                    : json["city"],
        distance                : json["distance"].toDouble(),
        logo                    : json["logo"],
      );
    }

}

我来打个电话

Future<Shops> getShops(var userLatitude, var userLongitude) async {
      final response =
          await http.get('https://dry-plateau-30024.herokuapp.com/v1/front/merchants?lat=$userLatitude&lng=$userLongitude');
    
      if (response.statusCode == 200) {
        
        return Shops.fromJson(json.decode(response.body));
      } else {
        
        throw Exception('Failed to load Shops');
      }
    }

所以我显示结果

    FutureBuilder<Shops>(
      future:
          getShops(services.userLatitude, services.userLongitude),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return Text(snapshot.data.name);
        } else if (snapshot.hasError) {
          return Information(
            title: 'No results found',
            subtitle: 'It seems there is nothing in your area',
            img: 'assets/nada.png',
          );
        } else {
          return CircularProgressIndicator();
        }
      },
    )

这会返回以下错误

_TypeError (type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'

我正在随着框架和飞镖语言的发展而发展,我遇到了这些错误,对你来说可能是新手。我会很感激一些帮助

【问题讨论】:

  • 你能告诉我们你的 JSON 是什么样子的吗? JSON 的设置方式将决定我们如何访问它。如果它是一个 List,那么你将不得不通过它进行映射。

标签: api flutter dart


【解决方案1】:

使用您提供的 JSON 的新链接。由于它是一个 list,我们必须将其转换为一个列表并通过它进行映射。在您只是返回 一个 Shops 实例Future&lt;Shops&gt; 而不是 Future&lt;List&lt;Shops&gt;&gt;

之前
class _CompaniesState extends State<Companies> {

  Future<List<Shops>> getShops(var userLatitude, var userLongitude) async {
      final response =
          await http.get("https://dry-plateau-30024.herokuapp.com/v1/front/merchants?lat=-27.4885846&lng=-58.9509858");
    
      if (response.statusCode == 200) {
        
        // typecasting it into a list
        var jsonData = json.decode(response.body) as List;

        // Map through it and return it
        return jsonData.map((shop) => Shops.fromJson(shop)).toList();

      } else {
        
        throw Exception('Failed to load Shops');
      }
    }



  @override
  Widget build(BuildContext context) {
    return Container(
        child: FutureBuilder<List<Shops>>(
      future: getShops(12, 20),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          // Accessing the first map from the list and getting the name key 
          return Text(snapshot.data[0].name);
        } else if (snapshot.hasError) {
          return null;
          return Information(
            title: 'No results found',
            subtitle: 'It seems there is nothing in your area',
            img: 'assets/nada.png',
          );
        } else {
          return CircularProgressIndicator();
        }
      },
    ));
  }
}

【讨论】:

  • 在理解我的错误后感谢您,这对我有用
猜你喜欢
  • 2021-06-27
  • 2022-12-02
  • 2017-08-24
  • 2019-04-06
  • 1970-01-01
  • 1970-01-01
  • 2022-11-09
  • 2022-12-02
  • 2022-12-02
相关资源
最近更新 更多