Flutter HTTP包没有响应 [英] Flutter HTTP package is not responding

查看:53
本文介绍了Flutter HTTP包没有响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我(使用python)制作的API到Flutter网站获取数据,但是我要求API响应的代码块未运行.我尝试了所有我知道的东西,但是给它的错误很奇怪.

I am trying to get data from an API I made(with python) to my Flutter website but the block of code in which I'm asking for API response is not running. I tried everything I knew but the error its giving is weird.

这是我遇到错误的代码:

Here is the code I'm getting error in :

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

const String url = 'http://localhost:5000/api?imagenumber=' ;

Future getImageIds(int image) async {

  Response response = await get( url + image.toString()) ;
  dynamic responsBody = response.body ;
  dynamic decodedData = jsonDecode(responsBody) ;
  return  decodedData ;
}

顺便说一下,该API目前正在localhost上运行.

By the way, the API is running on localhost(currently).

这是错误:

Performing hot restart...
Syncing files to device Chrome...
Restarted application in 605ms.
This if from screen 1: 1 , 2 , 3
Error: XMLHttpRequest error.
    C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 906:28                get current
packages/http/src/browser_client.dart 84:22                                                                                    <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/zone.dart 1450:54                                              runUnary
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 143:18                                        handleValue
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 696:44                                        handleValueCallback
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 725:32                                        _propagateToListeners
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 519:7                                         [_complete]
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/stream_pipe.dart 61:11                                         _cancelAndValue
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/stream.dart 1302:7                                             <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 324:14  _checkAndCall
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 329:39  dcall
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/html/dart2js/html_dart2js.dart 37312:58                              <fn>


    at Object.createErrorWithStack (http://localhost:55081/dart_sdk.js:4361:12)
    at Object._rethrow (http://localhost:55081/dart_sdk.js:38188:16)
    at async._AsyncCallbackEntry.new.callback (http://localhost:55081/dart_sdk.js:38182:13)
    at Object._microtaskLoop (http://localhost:55081/dart_sdk.js:38014:13)
    at _startMicrotaskLoop (http://localhost:55081/dart_sdk.js:38020:13)
    at http://localhost:55081/dart_sdk.js:33517:9

忽略前四行

推荐答案

您需要启用跨域资源共享(CORS)才能发出请求.如果启用了CORS,您将不再收到此错误.我遇到了同样的问题,因此决定只通过云功能发出请求.您可以研究启用cors,也许有一种方法也可以在Google chrome中启用它进行测试.

You needs Cross-Origin Resource Sharing (CORS) enabled to make the request. If CORS is enabled you won't get this error anymore. I faced the same issue and decided to just make the request through a cloud function. You can look into enabling cors, there may be a way to enable it for testing as well in Google chrome.

添加:

<IfModule mod_headers.c>
   Header set Access-Control-Allow-Origin "*"
</IfModule>

到您的服务器 .htaccess

这篇关于Flutter HTTP包没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆