0

我正在尝试从 coinmarketcap api 获取数据,但我似乎收到了上述错误。包括的是我的 main.dart 代码,这是我第一次使用 Flutter/dart,所以我不太了解所有内容,我遵循了来自 Flutter 文档https://docs.flutter.dev/cookbook/networking/background-的指南解析,但我仍然遇到一些错误,然后我尝试通过遵循此NoSuchMethodError: Class'_InternalLinkedHashMap<String, dynamic>'has no instance method 'cast' with matching arguments

谁能帮我?

我得到的错误是第 22 行:

NoSuchMethodError (NoSuchMethodError: Class 'CastMap<String, dynamic, String, dynamic>' has no instance method 'call'.

接收方:'CastMap<String, dynamic, String, dynamic>' 的实例

我的飞镖文件:

import 'dart:async';
import 'dart:convert';
import 'dart:core';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

Future<List<Criptos>> fetchcriptos(http.Client client) async {
  final response = await client.get(
      Uri.parse(
          'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?id=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15'),
      headers: {
        "X-CMC_PRO_API_KEY": "ecfc8e0a-fd11-422d-8a7b-114e8b31e62c",
        "Accept": "application/json"
      });
  return compute(parsecriptos, response.body);
}

List<Criptos> parsecriptos(String responseBody) {
  final parsed = jsonDecode(responseBody).cast<String, dynamic>();

  return parsed<Criptos>((json) => Criptos.fromJson(json)).toList();
}

class Criptos {
  final int id;
  final String name;
  final String symbol;
  final int max_supply;
  final int cmc_rank;
  final int preco;

  const Criptos({
    required this.id,
    required this.name,
    required this.symbol,
    required this.max_supply,
    required this.cmc_rank,
    required this.preco,
  });

  factory Criptos.fromJson(Map<String, dynamic> json) {
    return Criptos(
        id: json['data']['id'] as int,
        name: json['data']['name'] as String,
        symbol: json['data']['symbol'] as String,
        max_supply: json['data']['max_supply'] as int,
        cmc_rank: json['data']['cmc_rank'] as int,
        preco: json['data']['quote']['USD']['price'] as int);
  }
}

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    const appTitle = 'Isolate Demo';

    return const MaterialApp(
      title: appTitle,
      home: MyHomePage(title: appTitle),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: FutureBuilder<List<Criptos>>(
        future: fetchcriptos(http.Client()),
        builder: (context, snapshot) {
          if (snapshot.hasError) {
            return const Center(
              child: Text('An error has occurred!'),
            );
          } else if (snapshot.hasData) {
            return ListaCriptos(cripto: snapshot.data!);
          } else {
            return const Center(
              child: CircularProgressIndicator(),
            );
          }
        },
      ),
    );
  }
}

class ListaCriptos extends StatelessWidget {
  const ListaCriptos({Key? key, required this.cripto}) : super(key: key);

  final List<Criptos> cripto;

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2,
      ),
      itemCount: cripto.length,
      itemBuilder: (context, index) {
        return Text(cripto[index].id);
      },
    );
  }
}

项目的 Github

4

0 回答 0