如何解决flutter apps http请求错误

How to solve flutter apps http request error

我试图使用 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”时,它必须 return 一个类型为“Welcome”的对象,在您的情况下,当 statusCode 不同于 200 时,它不会 return 任何东西,而且,您使用不 return 的变量,尝试使用此代码:

服务:

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