How Flutter can decode a very large json array, Flutter just decode the only first 20 items?

Issue

Hello I’ve an api that return a big list the min length of this list could be 100 or 120 element, and flutter decode only first 20 element is the list.
Now that it seems to you that I load a very large json array elements and don’t care about the api fast the req only takes 300 mille sec as possible The Api Is : https://newsapi.org/. I use http package for loading data and thats my code.

Future getEverythingFunc({@required String query}) async {
    http.Response response =  await http.get(Uri.parse(ApiReq.everyThingForSearchingUrl(query:query)));
    if (response.statusCode >= 200 && response.statusCode <= 300) {
      print(response.body);
      return everythingModel = everythingModelFromJson(response.body);
    } else if (response.statusCode >= 400 && response.statusCode <= 500) {
      throw "Err while getting data ${response.statusCode}";
    } else {
      throw 'Unknown Err ${response.statusCode}';
    }
  }

Thanks for reading and hope you help me

Solution

I don’t know how you are coding, but jsonDecode function is working fine. Have you read the doc from NewsAPI? when pageSize parameter is omitted the default is 20 results, and its maximum value for free subscription is 100. I’ve tested it, my code:

// @dart=2.13
import 'dart:async';
import 'dart:io';

import 'package:wnetworking/wnetworking.dart';

typedef JMap = Map<String, dynamic>;

class NewsAPI {
  static const _base = 'https://newsapi.org/v2';
  static const _apiKey = '111111111111111111111111111111';
  
  // 'pageSize' ranges from 0 to 100 for free subscription, more size is paid
  // source: https://newsapi.org/docs/endpoints/everything
  static FutureOr<void> fetchNews({required keyword, int pageSize=20}) async {
    final url = '$_base/everything?q=$keyword&pageSize=$pageSize&sortBy=publishedAt&apiKey=$_apiKey';
    List? news;

    stdout.write('Fetching news... ');
    await HttpReqService.getJson<JMap>(url)
      .then((response) {
        if (response != null) {
          news = response['articles'] as List;
        }
      })
      .whenComplete(() {
        print('done!');
        print('\nNews fetched: ${news!.length}');
      });
  }
}

void main(List<String> args) async {
  await NewsAPI.fetchNews(keyword: 'anime', pageSize: 100);
  print('Job done.');
}

Result:

Fetching news... done!

News fetched: 100
Job done.

Note

  • wnetworking package is not ready to publish yet, it contains operations related to API, etc. You can replace HttpReqService.getJson with your typical http.get but keep in mind the return value and exceptions.

Answered By – Ουιλιαμ Αρκευα

Answer Checked By – Jay B. (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.