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

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

问题描述

我正在尝试从我制作的 API(使用 python)获取数据到我的 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 正在本地主机上运行(当前).

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天全站免登陆