首页 > 解决方案 > Flutter 在 json 数据中获取和循环

问题描述

由于我是flutter的新手,我按照教程构建了一个新闻应用程序,所以我制作了获取数据的类并且一切正常,当我执行应用程序时,我在获取数据方法中出现错误!有人能解释一下这段代码有什么问题吗!!

错误代码:

[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>'

类代码:

class FetchDataClass {
  late String author;
  late String title;
  late String description;
  late String url;
  late String urlToImage;
  late String publishedAt;
  late String content;

      FetchDataClass(this.author, this.title, this.description, this.url, this.urlToImage,     this.publishedAt, this.content );
  FetchDataClass.fromJson(Map<String, dynamic> jsonData) {
    author = jsonData['author'];
    title = jsonData['title'];
    description = jsonData['description'];
    url = jsonData['url'];
    urlToImage = jsonData['urlToImage'];
    publishedAt = jsonData['publishedAt'];
    content = jsonData['content'];
  }

}

抓取数据:

List<FetchDataClass> listofdata = List<FetchDataClass>.empty();
  Future<List<FetchDataClass>> loadNews() async {
    var response = await http.get(Uri.parse('https://newsapi.org/v2/everything?q=coronavirus&from=2021-09-10&sortBy=publishedAt&apiKey='));
    List<FetchDataClass> news = List<FetchDataClass>.empty();
            if(response.statusCode == 200) {
      dynamic notesJson = json.decode(response.body);
      for(dynamic noteJson in notesJson) { /// here the issue
    print(11111);
    news.add(FetchDataClass.fromJson(noteJson));
      }
    }
    return news;

  }

  @override
  void initState() {
    loadNews().then((value) {setState(() {
      listofdata.addAll(value);
    });});
    super.initState();
  }

标签: jsonflutterdart

解决方案


如果您的 API 的所有数据都与模型匹配,您可以试试这个

class FetchDataClass {
  String author;
  String title;
  String description;
  String url;
  String urlToImage;
  String publishedAt;
  String content;

  FetchDataClass(
      {required this.author,
      required this.title,
      required this.description,
      required this.url,
      required this.urlToImage,
      required this.publishedAt,
      required this.content});
  factory FetchDataClass.fromJson(Map<String, dynamic> jsonData) {
    return FetchDataClass(
      author: jsonData['author'],
      title: jsonData['title'],
      description: jsonData['description'],
      url: jsonData['url'],
      urlToImage: jsonData['urlToImage'],
      publishedAt: jsonData['publishedAt'],
      content: jsonData['content'],
    );
  }
}

和获取数据服务

    List<FetchDataClass> listofdata = List<FetchDataClass>.empty();
      Future<List<FetchDataClass>> loadNews() async {
        var response = await http.get(Uri.parse('https://newsapi.org/v2/everything?q=coronavirus&from=2021-09-10&sortBy=publishedAt&apiKey='));
        List<FetchDataClass> news = List<FetchDataClass>.empty();
        if(response.statusCode == 200) {
          final notesJson = json.decode(response.body);
          ///final news = List<FetchDataClass>.from(
             /// notesJson.map((model) => FetchDataClass.fromJson(model)));
final news = FetchDataClass.fromJson(model);
        }
        return news;
      }

因为当您在模型类型中的 json 是 Map<String,dynamic> 但在获取数据时您设置了动态 notesJson,这是错误的类型。我为你修复了模型和服务调用 api,尝试一下,如果有问题你可以发短信给我帮助。


推荐阅读