-1

since im newbie in flutter i follow a tutorial to build a news app, so i made the class of fetching data and all its fine, when i'm executing the app i have the error in the getting data method ! can some one explain what'is wrong in this code !!

the error code :

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

the Class code :

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'];
  }

}

Fetshing data :

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();
  }
Ihab07
  • 29
  • 7
  • Please include the error you're getting. It probably indicates what's the exact issue and often it will even tell you directly how to fix it :) – Boaz Oct 10 '21 at 15:16
  • `[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type '_InternalLinkedHashMap' is not a subtype of type 'Iterable'` – Ihab07 Oct 10 '21 at 15:22
  • @Boaz i update the question – Ihab07 Oct 10 '21 at 15:23
  • @Ihab07 You should include the data which is returned by the HTTP request. As the error, it may like `{a:b,c:d}` (this's a map) instead of `[{a:b,c:d},{e:f,g:h}]` (this's a list which you want). You can also take a look at [this](https://stackoverflow.com/questions/51053954/how-to-deserialize-a-list-of-objects-from-json-in-flutter). – Lam Thanh Nhan Oct 10 '21 at 15:38

1 Answers1

0

If your API have all data match with model you can try this

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'],
    );
  }
}

and Fetch data service

    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;
      }

Cause when you json in model type is Map<String,dynamic> but in fetching data you set dynamic notesJson it's wrong type for this. I fixed for you model and service call api, try and if some issue you can text for me to help.

Ethan Thai
  • 204
  • 2
  • 6
  • its give me this error ` Unhandled Exception: type '(dynamic) => FetchDataClass' is not a subtype of type '(String, dynamic) => MapEntry' of 'transform'` – Ihab07 Oct 10 '21 at 16:01
  • final news = FetchDataClass.fromJson(notesJson) Change to this – Ethan Thai Oct 10 '21 at 16:05