Flutter HTTP 包没有响应
Flutter HTTP package is not responding
我正在尝试从我制作的 API 中获取数据(使用 python)到我的 Flutter 网站,但是我在其中请求 API 响应的代码块不是 运行。我尝试了我所知道的一切,但它给出的错误很奇怪。
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 在本地主机上是 运行(当前)。
And here is the error:
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
忽略前四行
您需要 Cross-Origin 启用资源共享 (CORS) 才能发出请求。如果启用了 CORS,您将不会再收到此错误。我遇到了同样的问题,决定只通过云功能发出请求。您可以查看启用 cors,可能还有一种方法可以在 Google chrome.
中启用它进行测试
添加:
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
到您的服务器.htaccess
我正在尝试从我制作的 API 中获取数据(使用 python)到我的 Flutter 网站,但是我在其中请求 API 响应的代码块不是 运行。我尝试了我所知道的一切,但它给出的错误很奇怪。
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 在本地主机上是 运行(当前)。
And here is the error:
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
忽略前四行
您需要 Cross-Origin 启用资源共享 (CORS) 才能发出请求。如果启用了 CORS,您将不会再收到此错误。我遇到了同样的问题,决定只通过云功能发出请求。您可以查看启用 cors,可能还有一种方法可以在 Google chrome.
中启用它进行测试添加:
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
到您的服务器.htaccess