-1

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

6
  • 1
    Is it possible to provide all of the returned JSON so we can try and see what goes wrong? I guess you have checked by printing the response.body and verified that the JSON are fully valid and does not contain any charset issues. May 22, 2021 at 22:23
  • Yes I printed and see that the response return it's full data back but the problem cause while parsing it just only accept the first 20 element or random 20 element and put them on my list in my model. May 22, 2021 at 23:01
  • Ok, have you tested where you have pasted the full JSON directly into your dart code? Just to verify that the issue is not about some other problem? May 22, 2021 at 23:06
  • Yes for sure! here's the problem everything works awesome everything is good the only strange thing that it only parsing 20 element into model's list so I ask my self the problem could be related to threading or the problem could happen because api licence ? but api if you checked it says for developer plan nothing to pay so that I think the problem could cause because I do everything in the main thread so I used compute method but same thing happen !. you could check my code on git send me you're mail or write it here to check my code if you wanna check it May 22, 2021 at 23:30
  • I am not really a Flutter developer but I can properly move your code to some normal Dart program and test the JSON parsing. You can find my mail on my Github profile (after you have logged in): github.com/julemand101 . I am a little busy tomorrow so I cannot guarantee I will look at it before monday. :) May 22, 2021 at 23:33

1 Answer 1

0

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.
1
  • Thanks a lot bro you solved my issue the only thing that i miss is reading 'pageSize' in docs, Thanks for your time May 23, 2021 at 2:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.