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

Nullable QueryMap fails to compile #343

Closed
techouse opened this issue Jul 8, 2022 · 0 comments
Closed

Nullable QueryMap fails to compile #343

techouse opened this issue Jul 8, 2022 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@techouse
Copy link
Collaborator

techouse commented Jul 8, 2022

Hi, I really love your package, however, I came across an issue where I wanted to pass a QueryMap as an optional/nullable value and this resulted in it simply being added to the list of $param using $params.addAll(theNullableQueryMap). This however fails, because a nullable value can not be added to a Map using Map.addAll.

Essentially this:

@Get(path: 'articles')
Future<Response<List<Article>>> getArticles({
  @Query('article_id') String? articleId,
  @Query('author_name') String? authorName,
  @Query('limit') int? limit = 10,
  @QueryMap() Map<String, dynamic>? filters,
});

results in

@override
Future<Response<List<Article>>> getArticles(
    {String? articleId,
    String? authorName,
    int? limit = 10,
    Map<String, dynamic>? filters}) {
  final $url = 'articles';
  final $params = <String, dynamic>{
    'article_id': articleId,
    'authorName': authorName,
    'limit': limit
  };
  $params.addAll(filters); // <-- this here will fail to compile because `filters` can't be nullable 
  final $request = Request('GET', $url, client.baseUrl, parameters: $params);
  return client.send<List<Article>, Article>($request);
}

It throws this compilation error:

Error: The argument type 'Map<String, dynamic>?' can't be assigned to the parameter type 'Map<String, dynamic>' because 'Map<String, dynamic>?' is nullable and 'Map<String, dynamic>' isn't.

- 'Map' is from 'dart:core'.
   $params.addAll(filters);
                  ^

I would propose it checks if filters is null before using $params.addAll

final $params = <String, dynamic>{
  'article_id': articleId,
  'authorName': authorName,
  'limit': limit
};
if (filters != null) {
  $params.addAll(filters);
}

or simply using something more elegant like:

final $params = <String, dynamic>{
  'article_id': articleId,
  'authorName': authorName,
  'limit': limit,
  ...?filters,
};
@techouse techouse changed the title Nullable QueryMap Nullable QueryMap fails to compile Jul 8, 2022
techouse added a commit to techouse/chopper that referenced this issue Jul 10, 2022
@techouse techouse self-assigned this Jan 8, 2023
@techouse techouse added the bug Something isn't working label Jan 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant