我在从laravel api获取数据时遇到问题(颤振)

时间:2019-09-06 18:15:04

标签: api flutter dart

我正在研究Flutter教程,并且我曾经学习过如何在Flutter和Laravel api之间进行处理,我使用了这段代码来获取数据,并且遇到了问题,

有我的主要代码

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);
}

这是错误日志

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): 

我尝试了许多解决方案,但失败了 有没有人可以帮助您?

编辑>> 我在返回语句之前为response.body添加了打印语句,这是错误日志

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): 

2 个答案:

答案 0 :(得分:1)

enter image description here

这是您的API响应,它是一个错误,而不是json形式,这就是为什么您会收到格式异常错误的原因,因为它是json格式以外的内容,但由于错误,它正在获取此格式。

答案 1 :(得分:0)

错误正在解码中,恰好在此行return json.decode(response.body);上。也许您的服务器正在以非json格式返回数据,或者在返回时出现某种语法错误。

您可以打印response.body以便我们看到返回的蜜蜂信息吗?