0

i'm studying Flutter tutorial and i used to learn how to deal between flutter and Laravel api , i used this code to get data , and i have an issue ,

there is my main code

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

void main() async {

  List data = await getData() ;
  print(data) ;

  runApp(MaterialApp(
    debugShowCheckedModeBanner: false,
    title: 'Waseem Ashraf',
    home: Scaffold(
      appBar: AppBar(
        title: Text('Hello !!'),
        backgroundColor: Colors.deepPurpleAccent,
      ),
      body: Center(
        child: Text('Waseem Ashraf'),
      ),
    ),
  ));
}

Future<List> getData() async {
  String myUrl = 'http://flutterapitutorial.codeforiraq.org/api/products/';
  http.Response response = await http.get(myUrl, headers: {
    'Accept': 'application/json',
    'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjliNTg2MWVlNTg0ZGJkZGI5MDc1NDc3YTQ1ZDQ3ZDM5NGNiMzU2ZGIxZjBhMjUwZDUyZjk0YmViNGQwOTM3NTI0ZTM0MGNhMzBiYWM5NDAwIn0.eyJhdWQiOiIxIiwianRpIjoiOWI1ODYxZWU1ODRkYmRkYjkwNzU0NzdhNDVkNDdkMzk0Y2IzNTZkYjFmMGEyNTBkNTJmOTRiZWI0ZDA5Mzc1MjRlMzQwY2EzMGJhYzk0MDAiLCJpYXQiOjE1NTg1NTMyMTMsIm5iZiI6MTU1ODU1MzIxMywiZXhwIjoxNTkwMTc1NjEzLCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.sFIfXVx72efT54J40TVkqh3rwMGW-anTulNMDnVvGh_eO_qz0oKRl56JYCBwPQchc7fTbG5ZkVwaf_oU85rzjq3hrgXaOIzOoaNYsAKTOpPVbPi26bqpMLCWFe26hZO3BmS_kCSSD_-WlYVOlEw5oXQt1_MHV1eBt0tbXFLkgNwvkFr9IOvySINVsDOVoCArvp2Cx-XYthIP-0JuC7yQny5byMKerRGDO8pIjKLnPTTi9YWo36KU1SlzqoK-IJrQFvi5ir-rKk93IFCXwNoRN9QwXATb_4uJJyhpv2WLtXQwpnlPFqQFad8L0I8y9pfyzXnDtl3Aq1G3OlZMHbKcXp4uV8uByuT7UzI_FW6a0ion3Id1P3wy65n-X2OW2rDH6cpoCaz5_yzkpUfeo5WQ0RpG7q_VbWon2rf2NpbV8Jyzg80Woz3eNaQPA8-hdR5qUeeGXXulwfcT_sQln2uBmC3Ke2gbI1cKrBa4gVFpip9055lhgXfKzBvNkhV2dUljawBGacb0p4C1irkz6ygTzMu_31r2KHuzXiKQvbaEmorHGOLdvrwr-L2cqUmM3_jeAMmrV2_Pe4nRJHsvOLOYpB6ELNdeX8_DhD7DWUa6pdeU2PpRsXvwaGLbAkah9z7hCa54HGCzSLJPhN813nTXHuK_biSxIlH5n3ruvHiP6Rw'
  });
  return json.decode(response.body);
}

and this is the error log

E/flutter ( 4800): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: FormatException: Unexpected character (at character 1)
E/flutter ( 4800): <br />
E/flutter ( 4800): ^
E/flutter ( 4800): 
E/flutter ( 4800): #0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1392:5)
E/flutter ( 4800): #1      _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1259:9)
E/flutter ( 4800): #2      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:924:22)
E/flutter ( 4800): #3      _parseJson (dart:convert-patch/convert_patch.dart:29:10)
E/flutter ( 4800): #4      JsonDecoder.convert (dart:convert/json.dart:493:36)
E/flutter ( 4800): #5      JsonCodec.decode (dart:convert/json.dart:151:41)
E/flutter ( 4800): #6      getData (package:api1laravel/main.dart:31:15)
E/flutter ( 4800): <asynchronous suspension>
E/flutter ( 4800): #7      main (package:api1laravel/main.dart:7:21)
E/flutter ( 4800): <asynchronous suspension>
E/flutter ( 4800): #8      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:216:25)
E/flutter ( 4800): #9      _rootRun (dart:async/zone.dart:1124:13)
E/flutter ( 4800): #10     _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter ( 4800): #11     _runZoned (dart:async/zone.dart:1516:10)
E/flutter ( 4800): #12     runZoned (dart:async/zone.dart:1500:12)
E/flutter ( 4800): #13     _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:208:5)
E/flutter ( 4800): #14     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
E/flutter ( 4800): #15     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12)
E/flutter ( 4800): 

i tried many solutions and it's failed is there any one can help ?

EDITED >> i added print statement for response.body before return statement and this is error log

I/flutter ( 5204): <br />
I/flutter ( 5204): <b>Fatal error</b>:  Cannot redeclare auth() (previously declared in /home3/ab54412/flutterapitutorial.codeforiraq.org/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:159) in <b>/home3/ab54412/flutterapitutorial.codeforiraq.org/config/filter.php(12) : eval()'d code</b> on line <b>2</b><br />
E/flutter ( 5204): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: FormatException: Unexpected character (at character 1)
E/flutter ( 5204): <br />
E/flutter ( 5204): ^
E/flutter ( 5204): 
E/flutter ( 5204): #0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1392:5)
E/flutter ( 5204): #1      _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1259:9)
E/flutter ( 5204): #2      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:924:22)
E/flutter ( 5204): #3      _parseJson (dart:convert-patch/convert_patch.dart:29:10)
E/flutter ( 5204): #4      JsonDecoder.convert (dart:convert/json.dart:493:36)
E/flutter ( 5204): #5      JsonCodec.decode (dart:convert/json.dart:151:41)
E/flutter ( 5204): #6      getData (package:api1laravel/main.dart:32:15)
E/flutter ( 5204): <asynchronous suspension>
E/flutter ( 5204): #7      main (package:api1laravel/main.dart:7:21)
E/flutter ( 5204): <asynchronous suspension>
E/flutter ( 5204): #8      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:216:25)
E/flutter ( 5204): #9      _rootRun (dart:async/zone.dart:1124:13)
E/flutter ( 5204): #10     _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter ( 5204): #11     _runZoned (dart:async/zone.dart:1516:10)
E/flutter ( 5204): #12     runZoned (dart:async/zone.dart:1500:12)
E/flutter ( 5204): #13     _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:208:5)
E/flutter ( 5204): #14     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
E/flutter ( 5204): #15     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12)
E/flutter ( 5204): 
1
  • This API is not working with your given information of headers. Please check you are providing necessary headers and body in the request or try using another API.I have added the screen shot of your API response fetched from POSTMAN in the answer section. Please have a look and let me know if i am helpful or not. Commented Sep 7, 2019 at 5:07

2 Answers 2

1

enter image description here

This is your API response which is a error and not in a json form , that's why you are getting format exception error as it was excepting a json format but due to error it was getting this format.

Sign up to request clarification or add additional context in comments.

3 Comments

thank u very much for your response , yeah that's right i think that API is not working , and i would kindly ask from u API ( Laravel API ) that i can complete my tutorial ,
done , thanks for u ,, how can i got Laravel API that allow me to finish my tutorial ?
You can use this API without headers to complete your tutorial(It contains a sample data): jsonplaceholder.typicode.com/posts/1
0

The error is on de decoding, precisaly on this line return json.decode(response.body);. Maybe your server are returning data in a format that's not json or theres some kind of syntax error on the return.

Could you print response.body so we can see what's beeing returned?

1 Comment

i do what u say , and i edited my topic , there is a new error log

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.