如何解决null'使用GetX中获取数据时错误?

发布于 2025-01-28 18:43:25 字数 1229 浏览 1 评论 0原文

我正在使用flutter教程,使用GetX软件包从后端获取数据。尽管遵循了教程步骤,但我无法从后端检索数据,相反,我在终端中遇到了“无效”错误。以下是我基于教程的实现,以及我观察到的响应。

代码:

// Flutter code: DataController.dart
import 'package:flutter_golang_yt/services/service.dart';
import 'package:get/get.dart';

class DataController extends GetxController {
  DataService service = DataService();
  bool _isLoading = false;
  List<dynamic> _myData = [];

  Future<void> getData() async {
    _isLoading = true;
    Response response = await service.getData();
    _isLoading = false;
    if (response.statusCode == 200) {
      _myData = response.body;
      print("Data received: $_myData");
    } else {
      print("Failed to fetch data: Status code ${response.statusCode}");
    }
    update();
  }
}

// Service code: services.dart
import 'package:get/get.dart';

class DataService extends GetConnect implements GetxService {
  Future<Response> getData() async {
    return await get("http://localhost:3306/gettasks", headers: {
      'Content-Type': 'application/json; charset=UTF-8'
    });
  }
}

错误描述:

运行应用程序时,预期的结果是从后端接收任务列表并打印它们。但是,我的flutter终端中的输出只是“ null”,表明没有收到数据或数据处理存在问题。

I'm following a Flutter tutorial to fetch data from a backend using the GetX package. Despite following the tutorial steps, I'm unable to retrieve data from the backend, and instead, I get a 'null' error in my terminal. Below is my implementation based on the tutorial, along with the responses I observed.

Code:

// Flutter code: DataController.dart
import 'package:flutter_golang_yt/services/service.dart';
import 'package:get/get.dart';

class DataController extends GetxController {
  DataService service = DataService();
  bool _isLoading = false;
  List<dynamic> _myData = [];

  Future<void> getData() async {
    _isLoading = true;
    Response response = await service.getData();
    _isLoading = false;
    if (response.statusCode == 200) {
      _myData = response.body;
      print("Data received: $_myData");
    } else {
      print("Failed to fetch data: Status code ${response.statusCode}");
    }
    update();
  }
}

// Service code: services.dart
import 'package:get/get.dart';

class DataService extends GetConnect implements GetxService {
  Future<Response> getData() async {
    return await get("http://localhost:3306/gettasks", headers: {
      'Content-Type': 'application/json; charset=UTF-8'
    });
  }
}

Error Description:

When I run the application, the expected result is to receive a list of tasks from the backend and print them. However, the output in my Flutter terminal is simply 'null', indicating no data is received or there is an issue with data handling.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感悟人生的甜 2025-02-04 18:43:25

您要做的第一件事是确定它是“响应”还是“响应”。 (打印出它们)

确定后,您将需要查看“ Get”的文档,这将告诉您该事物可能无效的情况。

另外,如果您在没有太多编码知识的情况下关注本教程,我建议您从更简单的内容开始。使用异步代码和大型第三方图书馆并不是一个很好的起点。

The first thing you want to do is determine whether it's "response" or "response.statusCode" which is null. (print them out)

Once you've determined that, you'll want to look at the documentation for "get" which will tell you the situations in which it that thing can be null.

Also, if you're following this tutorial without much coding knowledge, I'd recommend starting with something simpler. Playing with asynchronous code and big 3rd party libraries is not a great starting place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文