retrofit.dart
retrofit.dart copied to clipboard
Warning "Use 'const' for final variables initialized to a constant value" in Generated Code
Describe the Bug
In the generated code by Retrofit.dart
, there is a warning regarding a final variable that could be const. Specifically, the warning is "Use 'const' for final variables initialized to a constant value". The line of code generating this warning is:
final Map<String, dynamic>? _data = null;
To Reproduce
Steps to reproduce the behavior:
- Create an abstract class for your API calls with Retrofit annotations.
- Create methods without providing Fields params
- Run the code generation command for Retrofit (pub run build_runner build or pub run build_runner watch).
- Check the generated code.
- See the warning in your console or code editor.
Expected behavior
Generated code should be free of warnings to follow Dart's best practices. if we have no data/fields provided then _data
variable should be const not final
Full Code
part 'categories_api_client.g.dart';
@RestApi()
abstract class CategoriesApiClient {
factory CategoriesApiClient(Dio dio, {String baseUrl}) = _CategoriesApiClient;
@GET("category")
Future<List<CategoryResponse>> getCategories();
}
generated code:
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'categories_api_client.dart';
// **************************************************************************
// RetrofitGenerator
// **************************************************************************
// ignore_for_file: unnecessary_brace_in_string_interps,no_leading_underscores_for_local_identifiers
class _CategoriesApiClient implements CategoriesApiClient {
_CategoriesApiClient(
this._dio, {
this.baseUrl,
});
final Dio _dio;
String? baseUrl;
@override
Future<List<CategoryResponse>> getCategories() async {
const _extra = <String, dynamic>{};
final queryParameters = <String, dynamic>{};
final _headers = <String, dynamic>{};
final Map<String, dynamic>? _data = null;
final _result = await _dio
.fetch<List<dynamic>>(_setStreamType<List<CategoryResponse>>(Options(
method: 'GET',
headers: _headers,
extra: _extra,
)
.compose(
_dio.options,
'category',
queryParameters: queryParameters,
data: _data,
)
.copyWith(
baseUrl: _combineBaseUrls(
_dio.options.baseUrl,
baseUrl,
))));
var value = _result.data!
.map(
(dynamic i) => CategoryResponse.fromJson(i as Map<String, dynamic>))
.toList();
return value;
}
RequestOptions _setStreamType<T>(RequestOptions requestOptions) {
if (T != dynamic &&
!(requestOptions.responseType == ResponseType.bytes ||
requestOptions.responseType == ResponseType.stream)) {
if (T == String) {
requestOptions.responseType = ResponseType.plain;
} else {
requestOptions.responseType = ResponseType.json;
}
}
return requestOptions;
}
String _combineBaseUrls(
String dioBaseUrl,
String? baseUrl,
) {
if (baseUrl == null || baseUrl.trim().isEmpty) {
return dioBaseUrl;
}
final url = Uri.parse(baseUrl);
if (url.isAbsolute) {
return url.toString();
}
return Uri.parse(dioBaseUrl).resolveUri(url).toString();
}
}
@ahmadjz
The same phenomenon occurred to me as well.
Although it is not a fundamental solution, you can exclude it from the flutter analyze
inspection by adding the following to analysis_options.yaml
.
How about this?
analyzer:
exclude:
- "**/*.g.dart"
- "**/*.freezed.dart"
errors:
invalid_annotation_target: ignore
Reference: https://zenn.dev/purigen/articles/23045ba53384f6
I know it's been a while but had this been fixed in later versions?