Home > Software design >  Flutter get return empty body but it is not empty
Flutter get return empty body but it is not empty

Time:03-16

as I wrote in the title I'm trying to obtain data from a server but the method http.get(Uri.parse(url)) returns empty body and od course the error caused by trying to decode an empty body, But if I copy paste the url used before I can see the json content. How is it possible?

The function I'm using is:

Future getData ( ) async {
     String url = 'http://www.viaggiatreno.it/viaggiatrenonew/resteasy/viaggiatreno/andamentoTreno/...';
        http.Response response = await http.get(Uri.parse(url));
      
     if (response.statusCode == 204) { 
       var data = response.body; 
       print("data are : $data");
       return jsonDecode(data);
     } else {
       print("unable to load data"); 
     }
 }

This is a little portion of what I should see with this command: print("data are : $data");

{
   "tipoTreno":"PG",
   "orientamento":"B",
   "codiceCliente":1,
   "fermateSoppresse":[
      
   ],
   "dataPartenza":null,
   "fermate":[
      {
         "orientamento":"B",
         "kcNumTreno":null,
         "stazione":"VENEZIA SANTA LUCIA",
         "id":"S02593",
         "listaCorrispondenze":null,
         "programmata":1646663160000,
         "programmataZero":null,
         "effettiva":1646663160000,
         "ritardo":0,
         "partenzaTeoricaZero":1646663160000,
         "arrivoTeoricoZero":null,
         "partenza_teorica":1646663160000,
         "arrivo_teorico":null,
         "isNextChanged":false,
         "partenzaReale":1646663160000,
         "arrivoReale":null,
         "ritardoPartenza":0,
         "ritardoArrivo":0,
         "\"pro ...........}"

so as you can see the body is what you want but not empty, and it is in json format. Can anyone help me? I can't solve this issue.

Output is:

I/flutter (3177): data are:
E/flutter ( 3177): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: FormatException: Unexpected end of input (at character 1)
E/flutter ( 3177): 
E/flutter ( 3177): ^
E/flutter ( 3177): 
E/flutter ( 3177): #0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1405:5)
E/flutter ( 3177): #1      _ChunkedJsonParser.close (dart:convert-patch/convert_patch.dart:523:7)
E/flutter ( 3177): #2      _parseJson (dart:convert-patch/convert_patch.dart:41:10)
E/flutter ( 3177): #3      JsonDecoder.convert (dart:convert/json.dart:612:36)
E/flutter ( 3177): #4      JsonCodec.decode (dart:convert/json.dart:216:41)
E/flutter ( 3177): #5      jsonDecode (dart:convert/json.dart:155:10)
E/flutter ( 3177): #6      NetworkHelper.ottieniTutteInfo (package:agenda_ferrovia/utilities/networkConnection.dart:36:14)
E/flutter ( 3177): <asynchronous suspension>
E/flutter ( 3177): #7      _ServizioUIAggiungiTrenoState.build.ottieniOrigine (package:agenda_ferrovia/screens/servizioUI/servizioUI_singolo.dart:110:33)
E/flutter ( 3177): <asynchronous suspension>
E/flutter ( 3177): #8      _ServizioUIAggiungiTrenoState.build.<anonymous closure> (package:agenda_ferrovia/screens/servizioUI/servizioUI_singolo.dart:231:23)
E/flutter ( 3177): <asynchronous suspension>
E/flutter ( 3177): 

CodePudding user response:

Thanks all for the support! I've found the issue! Some part of the 'url's String' comes from another call. Inside a variable there was the "hidden chars" \n. That was the cause of the issue -.- I've solved removing all chars except letters before save into variable used for the 'url'. I hope that my English was clear enough to understand.

Bye!

  • Related