Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs for merging between Options and BaseOptions are not clear #2194

Closed
feinstein opened this issue Apr 20, 2024 · 12 comments · Fixed by #2240
Closed

Docs for merging between Options and BaseOptions are not clear #2194

feinstein opened this issue Apr 20, 2024 · 12 comments · Fixed by #2240
Labels
e: documentation Improvements or additions to documentation p: dio Targeting `dio` package

Comments

@feinstein
Copy link

Request Statement

I want to reuse my dio instance, to take advantage of all the interceptors, timeouts and other things that were configured inside it. I have one HTTP GET request that needs to be done in another base URL tough, so I need to modify the base URL and the responseType. I tried this, but it doesn't work:

    final options = Options().compose(dio.options.copyWith(baseUrl: '', responseType: ResponseType.bytes), url);
    final response = await dio.get<Uint8List?>(url, options: options);

The idea was to create a new options object that has all the current options of my dio instance, just changing the baseUrl and responseType. This doesn't work because compose returns a RequestOptions and get needs an Options object.

The kinds of objects that dio creates limits the flexibility of the package and makes the API kinda confuse.

The only way I managed to get this to work was by doing:

final options = Options().compose(
      dio.options.copyWith(
        baseUrl: '',
        method: 'GET',
        responseType: ResponseType.bytes,
      ),
      url,
    );
    final response = await dio.fetch<Uint8List?>(options);

Solution Brainstorm

No response

@feinstein feinstein added the s: feature This issue indicates a feature request label Apr 20, 2024
@AlexV525
Copy link
Member

Pseudo:

const baseDartUrl = 'https://dart.dev';
const baseFlutterUrl = 'https://flutter.dev';

void main() async {
  final dio = Dio(BaseOptions(baseUrl: baseDartUrl));
  final requestWithDart = await dio.get('/');
  final requestWithFlutter = await dio.get('$baseFlutterUrl/');
}

@feinstein
Copy link
Author

feinstein commented Apr 20, 2024

I have a genetic method that makes requests to a complete url variable. I remember passing the variable to get and it would append my base url to the url and it would fail.

Also, how do you change the ResponseType?

@AlexV525
Copy link
Member

I have a genetic method that makes requests to a complete url variable. I remember passing the variable to get and it would append my base url to the url and it would fail.

Would be good if you could provide a reproducible case.

Also, how do you change the ResponseType?

Each request can specify ResponseType in their Options.

@feinstein
Copy link
Author

But that option I am proving is a new option, so wouldn't it override the values in my default option I used for constructing the dio instance?

@AlexV525
Copy link
Member

But that option I am proving is a new option, so wouldn't it override the values in my default option I used for constructing the dio instance?

Of course Options overrides BaseOptions, but it only happens in that request, rather than further requests.

@feinstein
Copy link
Author

But I want that request to carry all the options provided in the base options, only overriding the ones I need to change, not all of them.

@AlexV525
Copy link
Member

I'm wondering if you really tried to use Options. It works just as you want it to. :)

Given the above conversation, I think your usage might be invalid somehow. I cannot provide suggestions unless there is an actual problem with options, and please provide a reproducible example to explain your demand.

@AlexV525 AlexV525 added h: need more info Further information is requested and removed s: feature This issue indicates a feature request labels Apr 20, 2024
@feinstein
Copy link
Author

@AlexV525 I tested and indeed it worked, I think the docs got me confused, the docs say:

Each Dio instance has a base config for all requests made by itself, and we can override the base config with Options when make a single request.

The word "override" implies that all my Base Options are gone. I thought I would have to recreate all the configurations myself, from both options objects.

I would recommend some more details to ba added to this doc, explaining the options objects are merged, where the items defined inside the new options object will override the fields defined inside the base options.

@AlexV525
Copy link
Member

AlexV525 commented Apr 22, 2024

Each Dio instance has a base config for all requests made by itself, and we can override the base config with Options when make a single request.

The word "override" implies that all my Base Options are gone. I thought I would have to recreate all the configurations myself, from both options objects.

I'm not sure it implies the idea you've mentioned. No default values were given in the Options, thus nothing would be override when you are using an empty Options. https://pub.dev/documentation/dio/latest/dio/Options-class.html

@feinstein
Copy link
Author

I know that's how the code looks like, but reading the docs that's not clear. The internal code could be easily overriding things with nulls.

@feinstein
Copy link
Author

Override means substitute, merge means combine both into one.

@AlexV525 AlexV525 changed the title Dio has limited configurability without using fetch Docs for merging between Options and BaseOptions are not clear Apr 22, 2024
@AlexV525 AlexV525 added e: documentation Improvements or additions to documentation p: dio Targeting `dio` package and removed h: need more info Further information is requested labels Apr 22, 2024
@kuhnroyal
Copy link
Member

Yea, I can see that this might be confusing. Feel free to create a PR to update the documentation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
e: documentation Improvements or additions to documentation p: dio Targeting `dio` package
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants