NoSuchMethodError (NoSuchMethodError: Class 'CastMap'没有实例方法'call'

发布于 2025-01-11 18:57:20 字数 4116 浏览 0 评论 0原文

我正在尝试从 coinmarketcap api 获取数据,但我似乎收到了上述错误。包含的是我的 main.dart 代码,这是我第一次使用 flutter/dart,所以我不太了解所有内容,我遵循了 flutter 文档 https://docs.flutter.dev/cookbook/networking/background-parsing ,但我仍然遇到一些错误,然后我尝试解决通过遵循此NoSuchMethodError: Class'_InternalLinkedHashMap'没有实例方法'cast ' 具有匹配的参数

谁能帮助我?

我得到的错误是第 22 行:

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

Receiver: Instance of 'CastMap'

我的飞镖文件:

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

I am trying o get data from coinmarketcap api but i seem to be getting the error above. included is my main.dart code, this is my first time using flutter/dart, so i don't quite understand everything, i followed this guide from flutter docs https://docs.flutter.dev/cookbook/networking/background-parsing , but i still got some errors that i then tried to solve by following this NoSuchMethodError: Class'_InternalLinkedHashMap<String, dynamic>'has no instance method 'cast' with matching arguments

Can anyone help me?

The error i get is this on line 22:

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

Receiver: Instance of 'CastMap<String, dynamic, String, dynamic>'

My dart file:

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 of the project

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文