trevorwang / retrofit.dart

retrofit.dart is an dio client generator using source_gen and inspired by Chopper and Retrofit.
https://mings.in/retrofit.dart/
MIT License
1.09k stars 250 forks source link

`value` as kwarg of endpoint #661

Open dickermoshe opened 8 months ago

dickermoshe commented 8 months ago

Describe the bug You cant use value as a keyword argument in a endpoint. This is because the generated function declares one internally

The final variable 'value' can't be read because it's potentially unassigned at this point.
Ensure that it is assigned on necessary execution paths.dart[read_potentially_unassigned_final](https://dart.dev/diagnostics/read_potentially_unassigned_final)
Local variable 'value' can't be referenced before it is declared.
Try moving the declaration to before the first use, or renaming the local variable so that it doesn't hide a name from an enclosing scope.dart[referenced_before_declaration](https://dart.dev/diagnostics/referenced_before_declaration)

To Reproduce Run generator on this:

import 'package:dio/dio.dart';
import 'package:retrofit/retrofit.dart';

@RestApi()
abstract class ClientClient {
  factory ClientClient(Dio dio, {String? baseUrl}) = _ClientClient;

  @GET('/uuid')
  Future<String> getUuid({
    @Query('value') required String value,
  });
}

Creates this:

  @override
  Future<String> getUuid({required String value}) async {
    final _extra = <String, dynamic>{};
    final queryParameters = <String, dynamic>{r'value': value}; // BUG, value is declared later
    final _headers = <String, dynamic>{};
    const Map<String, dynamic>? _data = null;
    final _result = await _dio.fetch<String>(_setStreamType<String>(Options(
      method: 'GET',
      headers: _headers,
      extra: _extra,
    )
        .compose(
          _dio.options,
          '/uuid',
          queryParameters: queryParameters,
          data: _data,
        )
        .copyWith(
            baseUrl: _combineBaseUrls(
          _dio.options.baseUrl,
          baseUrl,
        ))));
    final value = _result.data!; // <<< Should be named `_value`
    return value;
  }
dickermoshe commented 8 months ago

We could wait for them to fix, but I would rather rename it