Having issues converting api json stream into an array in Flutter

188 Views Asked by At

The django api I am working with has the following actions user api (url = website/user/):

action: CREATE_USER
    necessary params: action, email_address, password
    note: action = 0

action: LOGIN
    necessary params: action, email_address, password
    note: action = 1

action: CHANGE_PASSWORD
    necessary params: action, email_address, password, new_password
    note: action = 2

action: DELETE_USER
    necessary params: action, email_address, password
    note: action = 3

entry api (url = website/entry/):

action: MAIN_PAGE
    necessary params: action
    note: action = 0

action: SEARCH_KEYWORD
    necessary params: action, keyword
    note: action = 1

action: FILTER_YEAR
    necessary params: action, first_year, second_year
    note: action = 0
    note: if filter is for 1 year, first_year and second_year should be equal

archive api (url = website/archive/):

action: CREATE_ARCHIVE
    necessary params: action, email_address, password, keyword, frequency
    note: action = 0
    note: frequency has to be 'daily', 'week', 'biweek', or 'month'

action: DISPLAY_USER_ARCHIVES
    necessary params: action, email_address, password
    note: action = 1
    note: to return entries pulled from archive, use action SEARCH_KEYWORD using the keyword saved in the archive

action: DELETE_ARCHIVE
    necessary params: action, email_address, password, keyword
    note: action = 2

action: UPDATE_FREQUENCY
    necessary params: action, email_address, password, keyword, new_frequency
    note: action = 3
    note: frequency has to be 'daily', 'week', 'biweek', or 'month'

I can receive entries in json format with the following django req: https://mapone-api.herokuapp.com/entry/?action=0&format=json

this gives:

[{"entry_id":1,"source_name":"Springer","source_link":"https://link.springer.com/article/10.1007/s11214-004-1456-7","article_title":"Cassini Imaging Science: Instrument Characteristics And Anticipated Scientific Investigations At Saturn","publication_date":"2004-11-01","author_list":"Carolyn C. Porco, Robert A. West, Steven Squyres","map_body":"Saturn","map_scale":null},{"entry_id":7,"source_name":"Springer","source_link":"https://link.springer.com/article/10.1186/s40645-020-0323-9","article_title":"Cold-based glaciation of Pavonis Mons, Mars: evidence for moraine deposition during glacial advance","publication_date":"2020-03-12","author_list":"Reid A. Parsons, Tomohiro Kanzaki, Ryodo Hemmi","map_body":"Pavonis Mons Mars","map_scale":null},{"entry_id":4,"source_name":"Springer","source_link":"https://link.springer.com/article/10.5047/eps.2011.10.002","article_title":"Erratum to: Influences of Venus\xe2\x80\x99 topography on fully developed superrotation and near-surface flow","publication_date":"2015-02-13","author_list":"Masaru Yamamoto, Masaaki Takahashi","map_body":"Venus","map_scale":null},{"entry_id":9,"source_name":"Springer","source_link":"https://link.springer.com/article/10.1007/BF00312318","article_title":"Evolution of the Olympus Mons Caldera, Mars","publication_date":"1992-07-01","author_list":"Peter J Mouginis-Mark, Mark S Robinson","map_body":"Pavonis Mons Mars","map_scale":null},{"entry_id":3,"source_name":"Springer","source_link":"https://link.springer.com/article/10.1007/s10686-021-09772-2","article_title":"Exploration of Enceladus and Titan: investigating ocean worlds\xe2\x80\x99 evolution and habitability in the Saturn system","publication_date":"2021-07-22","author_list":"Giuseppe Mitri, Jason Barnes, Athena Coustenis","map_body":"Saturn","map_scale":null},{"entry_id":8,"source_name":"Springer","source_link":"https://link.springer.com/article/10.1007/s12040-021-01672-5","article_title":"Landform evolution of Tharsis Montes and Olympus Mons of Mars: Insights from morphometric, hypsometric and chronologic evidences","publication_date":"2021-08-21","author_list":"Adnan Ahmad, Archana M Nair","map_body":"Pavonis Mons Mars","map_scale":null},{"entry_id":5,"source_name":"Springer","source_link":"https://link.springer.com/article/10.1007/BF00142388","article_title":"Processes of crustal formation and evolution on Venus: An analysis of topography, hypsometry, and crustal thickness variations","publication_date":"1990-07-01","author_list":"James W. Head","map_body":"Venus","map_scale":null},{"entry_id":6,"source_name":"Springer","source_link":"https://link.springer.com/article/10.1007/BF00119613","article_title":"The global and local correlation between the Venus gravity field and its topography","publication_date":"1992-05-01","author_list":"Milan Burša, Zdislav šíma, Jan Kostelecký","map_body":"Venus","map_scale":null},{"entry_id":2,"source_name":"Springer","source_link":"https://link.springer.com/article/10.1007/BF00931667","article_title":"The shape of the small satellites of Saturn: Gravitational equilibrium vs solid-state strength","publication_date":"1983-06-01","author_list":"P. Farinella, A. Milani, A. M. Nobili","map_body":"Saturn","map_scale":null}]

The following dart code claims that the above entry is null when trying to access it and throws the following error: XMLRequesterror. This error also occurs when trying

consumeApi() async { Response publication = await HttpRequests.get("https://mapone-api.herokuapp.com/entry/?action=0&format=json");

  return publication.json;
 }

Future<http.Response> fetchAlbum() { return http.get(Uri.parse("https://mapone-api.herokuapp.com/entry/?action=0&format=json")); }

^ is what the official flutter documentation suggests. Everything I try seems to give an XML request error

I need the values in the json stream to be put in an array so I can place the values in a dataTable() and I also need to be able to handle login and password requests. What am I doing wrong here? I have tried multiple things to get this to work with very little success

1

There are 1 best solutions below

0
On

This works fine on my end using Dio package which is more powerful, and in my opinion easier to use.

import 'package:dio/dio.dart';

  Future<void> consumeApi() async {
    final dio = Dio();

    final response = await dio
        .get('https://mapone-api.herokuapp.com/entry/?action=0&format=json');

    final list = response.data as List;
    print(list); // prints the full response
  }

Using the standard Http package it would look like this.

import 'package:http/http.dart' as http;
import 'dart:convert';


 Future<void> consumeApi() async {
    final uri = Uri.parse(
        'https://mapone-api.herokuapp.com/entry/?action=0&format=json');

    final response = await http.get(uri);

    final list = json.decode(response.body) as List;

    print(list); // also prints the full response
 }