微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何解决flutter应用程序http请求错误

如何解决如何解决flutter应用程序http请求错误

我试图使用 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,};
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。