首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Api类型'List<dynamic>‘不是'Map<String,dynamic>’类型的子类型

Api类型'List<dynamic>‘不是'Map<String,dynamic>’类型的子类型
EN

Stack Overflow用户
提问于 2022-08-27 16:34:27
回答 1查看 38关注 0票数 0

我正在跟踪flutter的api集成文档,但是我得到了这个错误类型'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'

根据它的文档给出的示例响应api是这个链接到api

代码语言:javascript
复制
[
  {
    "category": "technology",
    "datetime": 1596589501,
    "headline": "Square surges after reporting 64% jump in revenue, more customers using Cash App",
    "id": 5085164,
    "image": "https://image.cnbcfm.com/api/v1/image/105569283-1542050972462rts25mct.jpg?v=1542051069",
    "related": "",
    "source": "CNBC",
    "summary": "Shares of Square soared on Tuesday evening after posting better-than-expected quarterly results and strong growth in its consumer payments app.",
    "url": "https://www.cnbc.com/2020/08/04/square-sq-earnings-q2-2020.html"
  },
  {
    "category": "business",
    "datetime": 1596588232,
    "headline": "B&G Foods CEO expects pantry demand to hold up post-pandemic",
    "id": 5085113,
    "image": "https://image.cnbcfm.com/api/v1/image/106629991-1595532157669-gettyimages-1221952946-362857076_1-5.jpeg?v=1595532242",
    "related": "",
    "source": "CNBC",
    "summary": "\"I think post-Covid, people will be working more at home, which means people will be eating more breakfast\" and other meals at home, B&G CEO Ken Romanzi said.",
    "url": "https://www.cnbc.com/2020/08/04/bg-foods-ceo-expects-pantry-demand-to-hold-up-post-pandemic.html"
  },
  {
    "category": "top news",
    "datetime": 1596584406,
    "headline": "Anthony Levandowski gets 18 months in prison for stealing Google self-driving car files",
    "id": 5084850,
    "image": "https://image.cnbcfm.com/api/v1/image/106648265-1596584130509-UBER-LEVANDOWSKI.JPG?v=1596584247",
    "related": "",
    "source": "CNBC",
    "summary": "A U.S. judge on Tuesday sentenced former Google engineer Anthony Levandowski to 18 months in prison for stealing a trade secret from Google related to self-driving cars months before becoming the head of Uber Technologies Inc's rival unit.",
    "url": "https://www.cnbc.com/2020/08/04/anthony-levandowski-gets-18-months-in-prison-for-stealing-google-self-driving-car-files.html"
  }
  }]

这是我从这里遵循的代码

新闻班:

代码语言:javascript
复制
class News {
  final int id;
  final String category;
  final String datetime;
  final String headline;
  final String image;
  final String source;
  final String related;
  final String summary;
  final String url;

  News(
      {required this.id,
      required this.category,
      required this.datetime,
      required this.headline,
      required this.image,
      required this.source,
      required this.related,
      required this.summary,
      required this.url});

  factory News.fromJson(Map<String, dynamic> json) {
    return News(
      id: json['id'],
      category: json['category'],
      datetime: json['datetime'],
      headline: json['headline'],
      image: json['image'],
      related: json['realted'],
      source: json['source'],
      summary: json['summary'],
      url: json['url'],
    );
  }
}

数据获取屏幕:

代码语言:javascript
复制
class MarketingNewsScreen extends StatefulWidget {
  const MarketingNewsScreen({Key? key}) : super(key: key);

  @override
  State<MarketingNewsScreen> createState() => _MarketingNewsScreenState();
}

class _MarketingNewsScreenState extends State<MarketingNewsScreen> {
  late Future<News> futureNews;
  Future<News> fetchNews() async {
    final response = await http.get(Uri.parse(
        'https://finnhub.io/api/v1/news?category=general&token=mytokenhiddenhere'));

    if (response.statusCode == 200) {
      // If the server did return a 200 OK response,
      // then parse the JSON.
      return News.fromJson(jsonDecode(response.body));
    } else {
      // If the server did not return a 200 OK response,
      // then throw an exception.
      throw Exception('Failed to load album');
    }
  }

  @override
  void initState() {
    super.initState();
    futureNews = fetchNews();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          centerTitle: true,
          automaticallyImplyLeading: false,
          leading: IconButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              icon: const Icon(Icons.arrow_back_ios)),
          title: const Text("Marketing News")),
      body: Center(
        child: FutureBuilder<News>(
          future: futureNews,
          builder: (context, abc) {
            if (abc.hasData) {
              return Text(abc.data!.category);
            } else if (abc.hasError) {
              print("${abc.error}");
              return Text("${abc.error}");
            }
            return const CircularProgressIndicator();
          },
        ),
      ),
    );
  }
}

根据文档,一切看起来都很好,但是它会导致错误,而不起作用。我是新手,特别是api方面,所以我不知道我在这里做了什么。如有任何帮助,将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-27 16:39:55

您的api结果是list,但是在您期望的模型类中,将模型类更改为:

代码语言:javascript
复制
static List<News> fromJson(List _list) {
    List<News> result = [];

    for (var item in _list) {
      var news = News(
      id: item['id'],
      category: item['category'],
      datetime: item['datetime'],
      headline: item['headline'],
      image: item['image'],
      related: item['realted'],
      source: item['source'],
      summary: item['summary'],
      url: item['url'],
    );
      result.add(news);
    }
    return result;
  }

并将FutureBuilder<News>替换为FutureBuilder<List<News>>,将所有Future<News>替换为Future<List<News>>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73512697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档