When using the / operator to construct URLs with path segments containing colons (e.g., "posts:today"), Flutter Data incorrectly processes the string, resulting in malformed URLs.
Steps to Reproduce
- Create an adapter with a custom action endpoint that uses colon notation:
@override
String urlForFindAll(Map<String, dynamic> params) {
final action = params['action'] as String?;
if (action != null) {
return '$type:$action'; // e.g., returns "posts:today"
}
return super.urlForFindAll(params);
}
- Call
findAll with an action parameter:
await findAll(params: {'action': 'today'});
- The URL construction in
remote_adapter.dart uses:
baseUrl.asUri / urlForFindAll(params) & params
Expected Behavior
When baseUrl.asUri is https://api.example.com/ and urlForFindAll(params) returns "posts:today", the resulting URL should be:
https://api.example.com/posts:today
Actual Behavior
The URL is incorrectly constructed as:
https://api.example.com/today
The part before the colon (posts:) is dropped.
Analysis
The issue appears to be in how the / operator handles strings with colons. It seems to be treating the colon as a special character and only keeping the part after it.
Workaround
Currently, we have to manually construct the URI to avoid this issue:
final pathWithAction = '$type:today';
final uri = baseUrl.asUri.replace(
path: '${baseUrl.asUri.path}/$pathWithAction',
queryParameters: queryParams,
);
Proposed Solutions
Fix the / operator: Update the URL path joining logic to properly handle colons in path segments. Colons are valid characters in URL paths when not used as scheme separators.
Environment
- Flutter Data version: 2.0.0
- Flutter version: 3.29.2
- Dart version: 3.7.2
Additional Context
This pattern of using colons in API paths is common in REST API design, particularly following Google's API design guide for custom methods. Many APIs use this pattern (e.g., /posts:publish, /users:verify, etc.), so proper support would be beneficial.
Related Code
The issue is in the URL construction at:
https://github.com/flutterdata/flutter_data/blob/master/lib/src/adapter/remote_adapter.dart
Specifically where it constructs: baseUrl.asUri / urlForFindAll(params) & params
When using the
/operator to construct URLs with path segments containing colons (e.g.,"posts:today"), Flutter Data incorrectly processes the string, resulting in malformed URLs.Steps to Reproduce
findAllwith an action parameter:remote_adapter.dartuses:Expected Behavior
When
baseUrl.asUriishttps://api.example.com/andurlForFindAll(params)returns"posts:today", the resulting URL should be:Actual Behavior
The URL is incorrectly constructed as:
The part before the colon (
posts:) is dropped.Analysis
The issue appears to be in how the
/operator handles strings with colons. It seems to be treating the colon as a special character and only keeping the part after it.Workaround
Currently, we have to manually construct the URI to avoid this issue:
Proposed Solutions
Fix the
/operator: Update the URL path joining logic to properly handle colons in path segments. Colons are valid characters in URL paths when not used as scheme separators.Environment
Additional Context
This pattern of using colons in API paths is common in REST API design, particularly following Google's API design guide for custom methods. Many APIs use this pattern (e.g.,
/posts:publish,/users:verify, etc.), so proper support would be beneficial.Related Code
The issue is in the URL construction at:
https://github.com/flutterdata/flutter_data/blob/master/lib/src/adapter/remote_adapter.dart
Specifically where it constructs:
baseUrl.asUri / urlForFindAll(params) & params