retrofit.dart icon indicating copy to clipboard operation
retrofit.dart copied to clipboard

Generated retrofit api class should extend `not implement` abstract class to allow provision for custom endpoints implementation

Open lordvidex opened this issue 3 years ago • 2 comments

Is your feature request related to a problem? Please describe. Currently it is impossible to provide default implementation for an endpoint that i don't want generated by retrofit_generator.

Describe the solution you'd like replacing implements with extends in the generated file. so that default implementation can be added directly in the abstract class api file.

lordvidex avatar May 13 '22 08:05 lordvidex

Hi, I found a workaround, if it's still relevant for you, at least until (if) this will be changed.

If you create a constructor in your RestApi class (empty private is ok), it will generate extends implementation. Something like this:

abstract class UserService {
  Future<User> fetchUser(String id);
}

@RestApi()
abstract class UserServiceImpl implements UserService {
  UserServiceImpl._();
  factory UserServiceImpl(Dio dio) => _UserServiceImpl._(dio);

  @override
  @GET('/users/{id}')
  Future<User> fetchUser(@Path('id') String id);
}

It will generate this:

class _UserServiceImpl extends UserServiceImpl {
  _UserServiceImpl._(this.dio) : super._();

  ...
}

leoshusar avatar Jun 07 '22 22:06 leoshusar

isnt this just... the way to do it? it needs documentation, but it works fine

TekExplorer avatar Dec 09 '22 04:12 TekExplorer