【问题标题】:flutter: Unhandled Exception: type 'Null' is not a subtype of type 'String'扑动:未处理的异常:类型\'Null\'不是类型\'String\'的子类型
【发布时间】:2023-02-03 13:26:13
【问题描述】:

我尝试使用 Moralis API 通过钱包地址调用 NFT,但出现以下错误。

Unhandled Exception: type 'Null' is not a subtype of type 'String'

这些是被指出有错误的代码行。

final meta = jsonDecode(map?['metadata']);

    meta_name = meta['name'] ?? '';
    meta_image = meta['image'] ?? '';
    meta_description = meta['description'] ?? '';
if (response.statusCode == 200) {
      nfts = jsonDecode(response.body)['result'].map<Nfts>((result) {
        return Nfts.fromMap(result);
      }).toList();
    }

第二个的完整代码如下。

class NftsProviders{
  Uri uri = Uri.parse('https://deep-index.moralis.io/api/v2/(personal metamask wallet address)/nft?chain=polygon&format=decimal');

  Future<List<Nfts>> getNfts() async {
    List<Nfts> nfts = [];

    final response = await http.get(uri, headers: {
      'accept': 'application/json',
      'X-API-Key' : 'o1g9ywaRjZvZaeaByxhZc7mFOBVVvDJEksU0jeZ8b34fNX03ISTc72fltfsAnuYG'
    });

    if (response.statusCode == 200) {
      nfts = jsonDecode(response.body)['result'].map<Nfts>((result) {
        return Nfts.fromMap(result);
      }).toList();
    }
    else {
      throw Exception('Failed to load NFT');
    }

    return nfts;
  }
}

我想根据Firebase调用的用户钱包地址请求NFT信息。 提前致谢。

【问题讨论】:

    标签: flutter firebase api dart nft


    【解决方案1】:

    你能分享 api 调用的响应吗,很可能在 JSON 解码中有错字

    meta_name = meta['name'] ?? '';
    meta_image = meta['image'] ?? '';
    meta_description = meta['description'] ?? '';
    

    尝试quicktype.io生成响应模型

    【讨论】:

      猜你喜欢
      • 2021-12-28
      • 1970-01-01
      • 2022-06-30
      • 2022-11-13
      • 1970-01-01
      • 2023-01-27
      • 2021-09-19
      • 2021-07-18
      相关资源
      最近更新 更多