1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] " '_Service.xxxx' isn't a valid override of 'Service.xxxx' " when generating...

Discussão em 'Mobile' iniciado por Stack, Outubro 15, 2024 às 20:22.

  1. Stack

    Stack Membro Participativo

    I'm trying to use Retrofit and its generator to create an API Client service for my Flutter application. But after I run the command dart run build_runner build and the new file is generated, it has an error in each of the implemented methods which says the override is invalid.

    This is the content of my API client service:

    import 'package:bookpal/data/constants.dart';

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

    part 'general_database_api_service.g.dart';

    @RestApi(baseUrl: baseUrl)
    abstract class GeneralDatabaseApiService {
    factory GeneralDatabaseApiService(Dio dio, {String baseUrl}) = _GeneralDatabaseApiService;

    @GET('/users/{id}')
    Future<HttpResponse<UserModel>> getUser({
    @Header('Content-Type') String contentType = contentType,
    @Path() String id
    });
    }


    And this is the generated code:

    // GENERATED CODE - DO NOT MODIFY BY HAND

    part of 'general_database_api_service.dart';

    // **************************************************************************
    // RetrofitGenerator
    // **************************************************************************

    // ignore_for_file: unnecessary_brace_in_string_interps,no_leading_underscores_for_local_identifiers

    class _GeneralDatabaseApiService implements GeneralDatabaseApiService {
    _GeneralDatabaseApiService(
    this._dio, {
    this.baseUrl,
    }) {
    baseUrl ??= 'https://my-api-server.com';
    }

    final Dio _dio;

    String? baseUrl;

    @override
    Future<HttpResponse<UserModel>> getUser({
    contentType = contentType,
    required id,
    }) async {
    const _extra = <String, dynamic>{};
    final queryParameters = <String, dynamic>{};
    final _headers = <String, dynamic>{r'Content-Type': contentType};
    _headers.removeWhere((k, v) => v == null);
    final Map<String, dynamic>? _data = null;
    final _result = await _dio.fetch<Map<String, dynamic>>(
    _setStreamType<HttpResponse<UserModel>>(Options(
    method: 'GET',
    headers: _headers,
    extra: _extra,
    contentType: contentType,
    )
    .compose(
    _dio.options,
    '/users/${id}',
    queryParameters: queryParameters,
    data: _data,
    )
    .copyWith(baseUrl: baseUrl ?? _dio.options.baseUrl)));
    final value = UserModel.fromJson(_result.data!);
    final httpResponse = HttpResponse(value, _result);
    return httpResponse;
    }

    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;
    }
    }


    I skipped the rest of the methods since they have pretty similar structures and it might be too much to paste them all. The related error is:

    '_GeneralDatabaseApiService.getUser' ('Future<HttpResponse<UserModel>> Function({String contentType, required String id})') isn't a valid override of 'GeneralDatabaseApiService.getUser' ('Future<HttpResponse<UserModel>> Function({String contentType, String id})').dartinvalid_override

    general_database_api_service.dart(23, 35): The member being overridden.


    What appears to be the problem? I'm using

    retrofit: '>=4.0.2 <5.0.0'

    and dev_dependencies

    retrofit_generator: '>=5.0.0 <6.0.0'
    build_runner: '>=2.3.0 <4.0.0'

    But I've also tried using the most recent versions, got the same issue.

    Continue reading...

Compartilhe esta Página