在 Flutter 中使用 Dio 库发送 GET 请求,可以按照以下方式发送请求:

首先,确保已经在项目中引入了 Dio 库。在'pubspec.yaml'文件中添加如下依赖:

dependencies:
  dio: ^4.0.0

然后,可以使用以下代码发送 GET 请求:

import 'package:dio/dio.dart';

void getCheckCode(String mobile) async {
  try {
    var dio = Dio();
    var url = 'https://your-api-url/api/v1/getCheckCode/$mobile';
    var response = await dio.get(url);

    // 处理响应数据
    print(response.data);
  } catch (e) {
    // 处理异常
    print(e);
  }
}

在上述代码中,我们创建了一个'Dio'实例,并指定了请求的 URL,其中'$mobile'是路径中的参数。然后,使用'await'关键字发送 GET 请求,并将响应结果存储在'response'变量中。最后,可以根据需要处理响应数据或异常。

请注意,在实际开发中,需要替换'https://your-api-url'为你实际的 API 地址。


原文地址: https://www.cveoy.top/t/topic/o9qh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录