chaturadilan / riverpod_repo_generator

Creates providers of Riverpod based on repositories
MIT License
0 stars 1 forks source link

Question: Usage of generics in method calls #1

Open nissaba opened 2 weeks ago

nissaba commented 2 weeks ago

I am porting my API from Swift to a Flutter app, and the calls are using generics to cut down on rewriting calls to various endpoints.

Using the riverbed_repo_generator example I made an example of what I am trying to achieve.

// File data_repo.dart

import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:riverpod_repo/annotations.dart';

part 'data_repo.g.dart';
part 'data_repo.repo.dart';

@Riverpod(keepAlive: true)
RepoData repoData(RepoDataRef ref) => RepoDataImpl();

@riverpodRepo
abstract class RepoData {
  Future<R?> get<T extends BaseRequest, R extends BaseResponse>(
      T request);
}

class RepoDataImpl implements RepoData {
  @override
  Future<R?> get<T extends BaseRequest, R extends BaseResponse>({required T request}) {
    // The implementation logic should be written in this section
  }
}

The problem is that in the generated code the provider for the get method cannot understand what R is.

error: The name 'R' isn't a type, so it can't be used as a type argument.

in the generated code

@ProviderFor(get)
final sendProvider = AutoDisposeFutureProvider<R?>.internal(
  get,
  name: r'getProvider',
  debugGetCreateSourceHash:
      const bool.fromEnvironment('dart.vm.product') ? null : _$getHash,
  dependencies: null,
  allTransitiveDependencies: null,
);

typedef GetRef = AutoDisposeFutureProviderRef<R?>;
// ignore_for_file: type=lint
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member

Is it possible to use generic methods? or is it a limitation on the generator that can't correctly construct the provider?

chaturadilan commented 19 hours ago

Hi,

I think, Rivepod does not support it

https://github.com/rrousselGit/riverpod/issues/1119