You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

更新开发环境后Flutter项目出现RequestOptions无法赋值给Options参数的问题求助

Fixing Dio's RequestOptions → Options Type Mismatch Error

Hey there, I've run into this exact issue with Dio 4.x before—let's get you sorted out quickly!

The Problem

The error is straightforward: in Dio 4.0.0, the options parameter of dio.get() expects an Options instance, but you're passing a RequestOptions instead. Even though they live in the same file, these are distinct types in Dio's API design:

  • Options is the public-facing class you should use to configure requests.
  • RequestOptions is mostly for internal Dio use, to encapsulate fully resolved request details after processing.

The Fix

Simply replace RequestOptions with Options in your code. All the properties you're setting (method, headers, extra, baseUrl) are supported by Options:

Future<List<dynamic>> getUserAllGames() async {
  const _extra = <String, dynamic>{};
  SharedPreferences pref = await SharedPreferences.getInstance();
  Map<String, String> headers = {
    HttpHeaders.contentTypeHeader: "application/json",
    HttpHeaders.authorizationHeader: "Bearer ${pref.getString("token")}",
  };
  
  // Swap RequestOptions for Options here
  final response = await _dio.get('game/all', options: Options(
      method: 'GET',
      headers: headers,
      extra: _extra,
      baseUrl: baseUrl) );
  
  print(response);
  return response.data;
}

Bonus: Cleaner Code

Since you're using a GET request (which is the default for dio.get()), you can even omit the method: 'GET' parameter. Plus, Dio's get() method accepts headers, extra, and baseUrl directly as top-level parameters, so you can simplify further:

Future<List<dynamic>> getUserAllGames() async {
  const _extra = <String, dynamic>{};
  SharedPreferences pref = await SharedPreferences.getInstance();
  Map<String, String> headers = {
    HttpHeaders.contentTypeHeader: "application/json",
    HttpHeaders.authorizationHeader: "Bearer ${pref.getString("token")}",
  };
  
  // No need to wrap in Options at all!
  final response = await _dio.get(
    'game/all',
    headers: headers,
    extra: _extra,
    baseUrl: baseUrl,
  );
  
  print(response);
  return response.data;
}

This should resolve the type mismatch error and get your request working as expected.

内容的提问来源于stack exchange,提问作者Rich

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 20:22:47