问题描述
我试图使用 http 请求显示来自 MysqL 的数据。但是数据没有进入我的应用程序并显示错误。 我从一开始就遵循了一些教程,但在获取时遇到了同样的问题
import 'package:http/http.dart' as http;
import 'package:test_project/constants/strings.dart';
import 'dart:convert';
import 'package:test_project/models/categoryData.dart';
class API_Manager {
Future<Welcome> getData() async {
var client = http.Client();
var categoryModel = null;
var response = await http.get(
'https://Flutter-storebd.000webhostapp.com/Flutter_food_app/api/config/getfood.PHP');
if (response.statusCode == 200) {
var jsonString = response.body;
var jsonMap = json.decode(jsonString);
var categoryModel = Welcome.fromJson(jsonMap);
}
return categoryModel;
}
}
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 1612:54 runUnary
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 152:18 handleValue
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 704:44
handleValueCallback
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 733:13
_propagatetoListeners
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 530: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 1219: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 37307:58 <fn>
at Object.createErrorWithStack (http://localhost:61409/dart_sdk.js:5345:12)
at Object._rethrow (http://localhost:61409/dart_sdk.js:39347:16)
at async._AsyncCallbackEntry.new.callback (http://localhost:61409/dart_sdk.js:39341:13)
at Object._microtaskLoop (http://localhost:61409/dart_sdk.js:39173:13)
at _startMicrotaskLoop (http://localhost:61409/dart_sdk.js:39179:13)
at http://localhost:61409/dart_sdk.js:34686:9
解决方法
当您使用“Future”时,它必须返回一个“Welcome”类型的对象,在您的情况下,当 statusCode 与 200 不同时它不会返回任何内容,并且您使用不返回的变量,请尝试使用这段代码:
服务:
class ApiManager {
Future<Welcome> getData() async {
var client = http.Client();
var categoryModel;
try {
var response = await client.get(
'https://flutter-storebd.000webhostapp.com/flutter_food_app/api/config/getfood.php');
if (response.statusCode == 200) {
var jsonString = response.body;
print(jsonString);
var jsonMap = json.decode(jsonString);
categoryModel = Welcome.fromJson(jsonMap);
return categoryModel;
} else {
// Other status code,you have to return anything
return null;
}
} catch (e) {
// Error when request is executed,you have to return anything
print(e);
return null;
}
}
}
模型:
class Welcome {
Welcome({
this.id,this.title,this.price,this.details,});
String id;
String title;
String price;
String details;
factory Welcome.fromJson(Map<String,dynamic> json) => Welcome(
id: json["id"],title: json["title"],price: json["price"],details: json["details"],);
Map<String,dynamic> toJson() => {
"id": id,"title": title,"price": price,"details": details,};
}