diff --git a/examples/counter/lib/main.g.dart b/examples/counter/lib/main.g.dart index 0144045dd..d7ccd81e6 100644 --- a/examples/counter/lib/main.g.dart +++ b/examples/counter/lib/main.g.dart @@ -1,7 +1,5 @@ // GENERATED CODE - DO NOT MODIFY BY HAND -// ignore_for_file: type=lint - part of 'main.dart'; // ************************************************************************** @@ -27,4 +25,5 @@ final counterProvider = AutoDisposeNotifierProvider.internal( ); typedef _$Counter = AutoDisposeNotifier; -// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, +// ignore_for_file: type=lint +// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member diff --git a/examples/pub/lib/detail.g.dart b/examples/pub/lib/detail.g.dart index 8fbeb7752..b87897904 100644 --- a/examples/pub/lib/detail.g.dart +++ b/examples/pub/lib/detail.g.dart @@ -32,8 +32,6 @@ class _SystemHash { } } -typedef FetchPackageDetailsRef = AutoDisposeFutureProviderRef; - /// See also [fetchPackageDetails]. @ProviderFor(fetchPackageDetails) const fetchPackageDetailsProvider = FetchPackageDetailsFamily(); @@ -80,10 +78,10 @@ class FetchPackageDetailsFamily extends Family> { class FetchPackageDetailsProvider extends AutoDisposeFutureProvider { /// See also [fetchPackageDetails]. FetchPackageDetailsProvider({ - required this.packageName, - }) : super.internal( + required String packageName, + }) : this._internal( (ref) => fetchPackageDetails( - ref, + ref as FetchPackageDetailsRef, packageName: packageName, ), from: fetchPackageDetailsProvider, @@ -95,10 +93,44 @@ class FetchPackageDetailsProvider extends AutoDisposeFutureProvider { dependencies: FetchPackageDetailsFamily._dependencies, allTransitiveDependencies: FetchPackageDetailsFamily._allTransitiveDependencies, + packageName: packageName, ); + FetchPackageDetailsProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.packageName, + }) : super.internal(); + final String packageName; + @override + Override overrideWith( + FutureOr Function(FetchPackageDetailsRef provider) create, + ) { + return ProviderOverride( + origin: this, + override: FetchPackageDetailsProvider._internal( + (ref) => create(ref as FetchPackageDetailsRef), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + packageName: packageName, + ), + ); + } + + @override + AutoDisposeFutureProviderElement createElement() { + return _FetchPackageDetailsProviderElement(this); + } + @override bool operator ==(Object other) { return other is FetchPackageDetailsProvider && @@ -114,6 +146,20 @@ class FetchPackageDetailsProvider extends AutoDisposeFutureProvider { } } +mixin FetchPackageDetailsRef on AutoDisposeFutureProviderRef { + /// The parameter `packageName` of this provider. + String get packageName; +} + +class _FetchPackageDetailsProviderElement + extends AutoDisposeFutureProviderElement + with FetchPackageDetailsRef { + _FetchPackageDetailsProviderElement(super.provider); + + @override + String get packageName => (origin as FetchPackageDetailsProvider).packageName; +} + String _$likedPackagesHash() => r'304a4def167e245812638cba776e8d5eb66d8844'; /// See also [likedPackages]. @@ -238,8 +284,8 @@ class PackageMetricsProvider extends AutoDisposeAsyncNotifierProviderImpl< /// /// Copied from [PackageMetrics]. PackageMetricsProvider({ - required this.packageName, - }) : super.internal( + required String packageName, + }) : this._internal( () => PackageMetrics()..packageName = packageName, from: packageMetricsProvider, name: r'packageMetricsProvider', @@ -250,10 +296,52 @@ class PackageMetricsProvider extends AutoDisposeAsyncNotifierProviderImpl< dependencies: PackageMetricsFamily._dependencies, allTransitiveDependencies: PackageMetricsFamily._allTransitiveDependencies, + packageName: packageName, ); + PackageMetricsProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.packageName, + }) : super.internal(); + final String packageName; + @override + Future runNotifierBuild( + covariant PackageMetrics notifier, + ) { + return notifier.build( + packageName: packageName, + ); + } + + @override + Override overrideWith(PackageMetrics Function() create) { + return ProviderOverride( + origin: this, + override: PackageMetricsProvider._internal( + () => create()..packageName = packageName, + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + packageName: packageName, + ), + ); + } + + @override + AutoDisposeAsyncNotifierProviderElement + createElement() { + return _PackageMetricsProviderElement(this); + } + @override bool operator ==(Object other) { return other is PackageMetricsProvider && other.packageName == packageName; @@ -266,15 +354,21 @@ class PackageMetricsProvider extends AutoDisposeAsyncNotifierProviderImpl< return _SystemHash.finish(hash); } +} + +mixin PackageMetricsRef + on AutoDisposeAsyncNotifierProviderRef { + /// The parameter `packageName` of this provider. + String get packageName; +} + +class _PackageMetricsProviderElement + extends AutoDisposeAsyncNotifierProviderElement with PackageMetricsRef { + _PackageMetricsProviderElement(super.provider); @override - Future runNotifierBuild( - covariant PackageMetrics notifier, - ) { - return notifier.build( - packageName: packageName, - ); - } + String get packageName => (origin as PackageMetricsProvider).packageName; } // ignore_for_file: type=lint // ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member diff --git a/examples/pub/lib/search.g.dart b/examples/pub/lib/search.g.dart index c691d3a49..16790d9ab 100644 --- a/examples/pub/lib/search.g.dart +++ b/examples/pub/lib/search.g.dart @@ -31,8 +31,6 @@ class _SystemHash { } } -typedef FetchPackagesRef = AutoDisposeFutureProviderRef>; - /// See also [fetchPackages]. @ProviderFor(fetchPackages) const fetchPackagesProvider = FetchPackagesFamily(); @@ -82,11 +80,11 @@ class FetchPackagesFamily extends Family>> { class FetchPackagesProvider extends AutoDisposeFutureProvider> { /// See also [fetchPackages]. FetchPackagesProvider({ - required this.page, - this.search = '', - }) : super.internal( + required int page, + String search = '', + }) : this._internal( (ref) => fetchPackages( - ref, + ref as FetchPackagesRef, page: page, search: search, ), @@ -99,11 +97,48 @@ class FetchPackagesProvider extends AutoDisposeFutureProvider> { dependencies: FetchPackagesFamily._dependencies, allTransitiveDependencies: FetchPackagesFamily._allTransitiveDependencies, + page: page, + search: search, ); + FetchPackagesProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.page, + required this.search, + }) : super.internal(); + final int page; final String search; + @override + Override overrideWith( + FutureOr> Function(FetchPackagesRef provider) create, + ) { + return ProviderOverride( + origin: this, + override: FetchPackagesProvider._internal( + (ref) => create(ref as FetchPackagesRef), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + page: page, + search: search, + ), + ); + } + + @override + AutoDisposeFutureProviderElement> createElement() { + return _FetchPackagesProviderElement(this); + } + @override bool operator ==(Object other) { return other is FetchPackagesProvider && @@ -120,5 +155,24 @@ class FetchPackagesProvider extends AutoDisposeFutureProvider> { return _SystemHash.finish(hash); } } + +mixin FetchPackagesRef on AutoDisposeFutureProviderRef> { + /// The parameter `page` of this provider. + int get page; + + /// The parameter `search` of this provider. + String get search; +} + +class _FetchPackagesProviderElement + extends AutoDisposeFutureProviderElement> + with FetchPackagesRef { + _FetchPackagesProviderElement(super.provider); + + @override + int get page => (origin as FetchPackagesProvider).page; + @override + String get search => (origin as FetchPackagesProvider).search; +} // ignore_for_file: type=lint // ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member diff --git a/packages/riverpod/lib/src/async_notifier/auto_dispose.dart b/packages/riverpod/lib/src/async_notifier/auto_dispose.dart index 0a6ed203d..e4d31a2bd 100644 --- a/packages/riverpod/lib/src/async_notifier/auto_dispose.dart +++ b/packages/riverpod/lib/src/async_notifier/auto_dispose.dart @@ -87,15 +87,17 @@ class AutoDisposeAsyncNotifierProviderImpl< @override AutoDisposeAsyncNotifierProviderElement createElement() { - return AutoDisposeAsyncNotifierProviderElement._(this); + return AutoDisposeAsyncNotifierProviderElement(this); } @override + @mustBeOverridden FutureOr runNotifierBuild(AsyncNotifierBase notifier) { return (notifier as AutoDisposeAsyncNotifier).build(); } /// {@macro riverpod.overridewith} + @mustBeOverridden Override overrideWith(NotifierT Function() create) { return ProviderOverride( origin: this, @@ -119,5 +121,6 @@ class AutoDisposeAsyncNotifierProviderElement< with AutoDisposeProviderElementMixin> implements AutoDisposeAsyncNotifierProviderRef { /// The [ProviderElementBase] for [AsyncNotifierProvider] - AutoDisposeAsyncNotifierProviderElement._(super._provider) : super._(); + @internal + AutoDisposeAsyncNotifierProviderElement(super._provider) : super(); } diff --git a/packages/riverpod/lib/src/async_notifier/auto_dispose_family.dart b/packages/riverpod/lib/src/async_notifier/auto_dispose_family.dart index 7534b5871..9ed8b808b 100644 --- a/packages/riverpod/lib/src/async_notifier/auto_dispose_family.dart +++ b/packages/riverpod/lib/src/async_notifier/auto_dispose_family.dart @@ -70,7 +70,7 @@ class AutoDisposeFamilyAsyncNotifierProviderImpl< @override AutoDisposeAsyncNotifierProviderElement createElement() { - return AutoDisposeAsyncNotifierProviderElement._(this); + return AutoDisposeAsyncNotifierProviderElement(this); } @override diff --git a/packages/riverpod/lib/src/async_notifier/base.dart b/packages/riverpod/lib/src/async_notifier/base.dart index e6a1e58ae..18631067c 100644 --- a/packages/riverpod/lib/src/async_notifier/base.dart +++ b/packages/riverpod/lib/src/async_notifier/base.dart @@ -131,15 +131,17 @@ class AsyncNotifierProviderImpl, T> @override AsyncNotifierProviderElement createElement() { - return AsyncNotifierProviderElement._(this); + return AsyncNotifierProviderElement(this); } @override + @mustBeOverridden FutureOr runNotifierBuild(AsyncNotifierBase notifier) { return (notifier as AsyncNotifier).build(); } /// {@macro riverpod.overridewith} + @mustBeOverridden Override overrideWith(NotifierT Function() create) { return ProviderOverride( origin: this, @@ -472,7 +474,9 @@ abstract class AsyncNotifierProviderElementBase< NotifierT extends AsyncNotifierBase, T> extends ProviderElementBase> with FutureHandlerProviderElementMixin { - AsyncNotifierProviderElementBase._(super._provider); + /// The element of [AsyncNotifierProvider]. + @internal + AsyncNotifierProviderElementBase(super._provider); final _notifierNotifier = ProxyElementValueNotifier(); @@ -501,9 +505,11 @@ abstract class AsyncNotifierProviderElementBase< class AsyncNotifierProviderElement, T> extends AsyncNotifierProviderElementBase implements AsyncNotifierProviderRef { - AsyncNotifierProviderElement._( + /// The element of [AsyncNotifierProvider]. + @internal + AsyncNotifierProviderElement( AsyncNotifierProviderBase super._provider, - ) : super._(); + ); @override void create({required bool didChangeDependency}) { diff --git a/packages/riverpod/lib/src/async_notifier/family.dart b/packages/riverpod/lib/src/async_notifier/family.dart index fb2981b7f..1160db44c 100644 --- a/packages/riverpod/lib/src/async_notifier/family.dart +++ b/packages/riverpod/lib/src/async_notifier/family.dart @@ -81,7 +81,7 @@ class FamilyAsyncNotifierProviderImpl, T, @override AsyncNotifierProviderElement createElement() { - return AsyncNotifierProviderElement._(this); + return AsyncNotifierProviderElement(this); } @override diff --git a/packages/riverpod/lib/src/future_provider/auto_dispose.dart b/packages/riverpod/lib/src/future_provider/auto_dispose.dart index 9f0ce11a9..f2eec475c 100644 --- a/packages/riverpod/lib/src/future_provider/auto_dispose.dart +++ b/packages/riverpod/lib/src/future_provider/auto_dispose.dart @@ -45,7 +45,7 @@ class AutoDisposeFutureProvider extends _FutureProviderBase @override AutoDisposeFutureProviderElement createElement() { - return AutoDisposeFutureProviderElement._(this); + return AutoDisposeFutureProviderElement(this); } @override @@ -75,9 +75,10 @@ class AutoDisposeFutureProviderElement extends FutureProviderElement with AutoDisposeProviderElementMixin> implements AutoDisposeFutureProviderRef { /// The [ProviderElementBase] for [FutureProvider] - AutoDisposeFutureProviderElement._( + @internal + AutoDisposeFutureProviderElement( AutoDisposeFutureProvider super._provider, - ) : super._(); + ) : super(); } /// The [Family] of an [AutoDisposeFutureProvider] diff --git a/packages/riverpod/lib/src/future_provider/base.dart b/packages/riverpod/lib/src/future_provider/base.dart index 67de97e61..0c9b3e142 100644 --- a/packages/riverpod/lib/src/future_provider/base.dart +++ b/packages/riverpod/lib/src/future_provider/base.dart @@ -65,7 +65,7 @@ class FutureProvider extends _FutureProviderBase FutureOr _create(FutureProviderElement ref) => _createFn(ref); @override - FutureProviderElement createElement() => FutureProviderElement._(this); + FutureProviderElement createElement() => FutureProviderElement(this); /// {@macro riverpod.overridewith} Override overrideWith(Create, FutureProviderRef> create) { @@ -88,7 +88,10 @@ class FutureProvider extends _FutureProviderBase class FutureProviderElement extends ProviderElementBase> with FutureHandlerProviderElementMixin implements FutureProviderRef { - FutureProviderElement._(_FutureProviderBase super._provider); + /// The element of a [FutureProvider] + @internal + // ignore: library_private_types_in_public_api + FutureProviderElement(_FutureProviderBase super._provider); @override Future get future { diff --git a/packages/riverpod/lib/src/notifier/auto_dispose.dart b/packages/riverpod/lib/src/notifier/auto_dispose.dart index bd8f0962b..b12205030 100644 --- a/packages/riverpod/lib/src/notifier/auto_dispose.dart +++ b/packages/riverpod/lib/src/notifier/auto_dispose.dart @@ -82,15 +82,17 @@ class AutoDisposeNotifierProviderImpl, T> @override AutoDisposeNotifierProviderElement createElement() { - return AutoDisposeNotifierProviderElement._(this); + return AutoDisposeNotifierProviderElement(this); } @override + @mustBeOverridden T runNotifierBuild(NotifierBase notifier) { return (notifier as AutoDisposeNotifier).build(); } /// {@macro riverpod.overridewith} + @mustBeOverridden Override overrideWith(NotifierT Function() create) { return ProviderOverride( origin: this, @@ -113,5 +115,6 @@ class AutoDisposeNotifierProviderElement, T> with AutoDisposeProviderElementMixin implements AutoDisposeNotifierProviderRef { /// The [ProviderElementBase] for [NotifierProvider] - AutoDisposeNotifierProviderElement._(super._provider) : super._(); + @internal + AutoDisposeNotifierProviderElement(super._provider); } diff --git a/packages/riverpod/lib/src/notifier/auto_dispose_family.dart b/packages/riverpod/lib/src/notifier/auto_dispose_family.dart index e04248915..f2c4882b3 100644 --- a/packages/riverpod/lib/src/notifier/auto_dispose_family.dart +++ b/packages/riverpod/lib/src/notifier/auto_dispose_family.dart @@ -66,7 +66,7 @@ class AutoDisposeFamilyNotifierProviderImpl, @override AutoDisposeNotifierProviderElement createElement() { - return AutoDisposeNotifierProviderElement._(this); + return AutoDisposeNotifierProviderElement(this); } @override diff --git a/packages/riverpod/lib/src/notifier/base.dart b/packages/riverpod/lib/src/notifier/base.dart index f3402348d..f5aea2b1a 100644 --- a/packages/riverpod/lib/src/notifier/base.dart +++ b/packages/riverpod/lib/src/notifier/base.dart @@ -147,7 +147,7 @@ class NotifierProviderImpl, T> @override NotifierProviderElement createElement() { - return NotifierProviderElement._(this); + return NotifierProviderElement(this); } @override @@ -155,11 +155,13 @@ class NotifierProviderImpl, T> _notifier(this); @override + @mustBeOverridden T runNotifierBuild(NotifierBase notifier) { return (notifier as Notifier).build(); } /// {@macro riverpod.overridewith} + @mustBeOverridden Override overrideWith(NotifierT Function() create) { return ProviderOverride( origin: this, @@ -179,7 +181,9 @@ class NotifierProviderImpl, T> /// The element of [NotifierProvider]. class NotifierProviderElement, T> extends ProviderElementBase implements NotifierProviderRef { - NotifierProviderElement._(NotifierProviderBase super._provider); + /// The element of [NotifierProvider]. + @internal + NotifierProviderElement(NotifierProviderBase super._provider); final _notifierNotifier = ProxyElementValueNotifier(); diff --git a/packages/riverpod/lib/src/notifier/family.dart b/packages/riverpod/lib/src/notifier/family.dart index bf33f6c07..96aa83f72 100644 --- a/packages/riverpod/lib/src/notifier/family.dart +++ b/packages/riverpod/lib/src/notifier/family.dart @@ -69,7 +69,7 @@ class FamilyNotifierProviderImpl, T, Arg> @override NotifierProviderElement createElement() { - return NotifierProviderElement._(this); + return NotifierProviderElement(this); } @override diff --git a/packages/riverpod/lib/src/provider/auto_dispose.dart b/packages/riverpod/lib/src/provider/auto_dispose.dart index 2b87dce9c..e0612671f 100644 --- a/packages/riverpod/lib/src/provider/auto_dispose.dart +++ b/packages/riverpod/lib/src/provider/auto_dispose.dart @@ -41,7 +41,7 @@ class AutoDisposeProvider extends InternalProvider { @override AutoDisposeProviderElement createElement() { - return AutoDisposeProviderElement._(this); + return AutoDisposeProviderElement(this); } /// {@macro riverpod.overridewith} @@ -68,8 +68,8 @@ class AutoDisposeProviderElement extends ProviderElement with AutoDisposeProviderElementMixin implements AutoDisposeProviderRef { /// The [ProviderElementBase] for [Provider] - AutoDisposeProviderElement._(AutoDisposeProvider super._provider) - : super._(); + @internal + AutoDisposeProviderElement(AutoDisposeProvider super._provider); } /// The [Family] of [AutoDisposeProvider] diff --git a/packages/riverpod/lib/src/provider/base.dart b/packages/riverpod/lib/src/provider/base.dart index db5ab5f68..868f567fa 100644 --- a/packages/riverpod/lib/src/provider/base.dart +++ b/packages/riverpod/lib/src/provider/base.dart @@ -55,7 +55,7 @@ class Provider extends InternalProvider State _create(ProviderElement ref) => _createFn(ref); @override - ProviderElement createElement() => ProviderElement._(this); + ProviderElement createElement() => ProviderElement(this); /// {@template riverpod.overridewith} /// Override the provider with a new initialization function. @@ -322,7 +322,8 @@ class Provider extends InternalProvider class ProviderElement extends ProviderElementBase implements ProviderRef { /// A [ProviderElementBase] for [Provider] - ProviderElement._(super._provider); + @internal + ProviderElement(super._provider); @override State get state => requireState; diff --git a/packages/riverpod/lib/src/stream_notifier/auto_dispose.dart b/packages/riverpod/lib/src/stream_notifier/auto_dispose.dart index 510094ce7..29a011b5d 100644 --- a/packages/riverpod/lib/src/stream_notifier/auto_dispose.dart +++ b/packages/riverpod/lib/src/stream_notifier/auto_dispose.dart @@ -83,7 +83,7 @@ class AutoDisposeStreamNotifierProviderImpl< @override AutoDisposeStreamNotifierProviderElement createElement() { - return AutoDisposeStreamNotifierProviderElement._(this); + return AutoDisposeStreamNotifierProviderElement(this); } @override @@ -94,6 +94,7 @@ class AutoDisposeStreamNotifierProviderImpl< } /// {@macro riverpod.overridewith} + @mustBeOverridden Override overrideWith(NotifierT Function() create) { return ProviderOverride( origin: this, @@ -117,5 +118,6 @@ class AutoDisposeStreamNotifierProviderElement< with AutoDisposeProviderElementMixin> implements AutoDisposeStreamNotifierProviderRef { /// The [ProviderElementBase] for [StreamNotifierProvider] - AutoDisposeStreamNotifierProviderElement._(super._provider) : super._(); + @internal + AutoDisposeStreamNotifierProviderElement(super._provider); } diff --git a/packages/riverpod/lib/src/stream_notifier/auto_dispose_family.dart b/packages/riverpod/lib/src/stream_notifier/auto_dispose_family.dart index 8005146de..7caf6470f 100644 --- a/packages/riverpod/lib/src/stream_notifier/auto_dispose_family.dart +++ b/packages/riverpod/lib/src/stream_notifier/auto_dispose_family.dart @@ -65,7 +65,7 @@ class AutoDisposeFamilyStreamNotifierProviderImpl< @override AutoDisposeStreamNotifierProviderElement createElement() { - return AutoDisposeStreamNotifierProviderElement._(this); + return AutoDisposeStreamNotifierProviderElement(this); } @override diff --git a/packages/riverpod/lib/src/stream_notifier/base.dart b/packages/riverpod/lib/src/stream_notifier/base.dart index 350bf4457..a895f59a4 100644 --- a/packages/riverpod/lib/src/stream_notifier/base.dart +++ b/packages/riverpod/lib/src/stream_notifier/base.dart @@ -101,7 +101,7 @@ class StreamNotifierProviderImpl, T> @override StreamNotifierProviderElement createElement() { - return StreamNotifierProviderElement._(this); + return StreamNotifierProviderElement(this); } @override @@ -112,6 +112,7 @@ class StreamNotifierProviderImpl, T> } /// {@macro riverpod.overridewith} + @mustBeOverridden Override overrideWith(NotifierT Function() create) { return ProviderOverride( origin: this, @@ -132,9 +133,11 @@ class StreamNotifierProviderImpl, T> class StreamNotifierProviderElement, T> extends AsyncNotifierProviderElementBase implements StreamNotifierProviderRef { - StreamNotifierProviderElement._( + /// The element of [StreamNotifierProvider]. + @internal + StreamNotifierProviderElement( StreamNotifierProviderBase super._provider, - ) : super._(); + ); @override void create({required bool didChangeDependency}) { diff --git a/packages/riverpod/lib/src/stream_notifier/family.dart b/packages/riverpod/lib/src/stream_notifier/family.dart index 1a0c3832f..ad855b512 100644 --- a/packages/riverpod/lib/src/stream_notifier/family.dart +++ b/packages/riverpod/lib/src/stream_notifier/family.dart @@ -77,7 +77,7 @@ class FamilyStreamNotifierProviderImpl, @override StreamNotifierProviderElement createElement() { - return StreamNotifierProviderElement._(this); + return StreamNotifierProviderElement(this); } @override diff --git a/packages/riverpod/lib/src/stream_provider/auto_dispose.dart b/packages/riverpod/lib/src/stream_provider/auto_dispose.dart index 025ca0719..5e999d599 100644 --- a/packages/riverpod/lib/src/stream_provider/auto_dispose.dart +++ b/packages/riverpod/lib/src/stream_provider/auto_dispose.dart @@ -43,7 +43,7 @@ class AutoDisposeStreamProvider extends _StreamProviderBase @override AutoDisposeStreamProviderElement createElement() { - return AutoDisposeStreamProviderElement._(this); + return AutoDisposeStreamProviderElement(this); } @override @@ -80,9 +80,9 @@ class AutoDisposeStreamProviderElement extends StreamProviderElement with AutoDisposeProviderElementMixin> implements AutoDisposeStreamProviderRef { /// The [ProviderElementBase] for [StreamProvider] - AutoDisposeStreamProviderElement._( + AutoDisposeStreamProviderElement( AutoDisposeStreamProvider super._provider, - ) : super._(); + ); } /// The [Family] of [AutoDisposeStreamProvider]. diff --git a/packages/riverpod/lib/src/stream_provider/base.dart b/packages/riverpod/lib/src/stream_provider/base.dart index 59d444a8c..545e2b62d 100644 --- a/packages/riverpod/lib/src/stream_provider/base.dart +++ b/packages/riverpod/lib/src/stream_provider/base.dart @@ -119,9 +119,10 @@ class StreamProvider extends _StreamProviderBase Stream _create(StreamProviderElement ref) => _createFn(ref); @override - StreamProviderElement createElement() => StreamProviderElement._(this); + StreamProviderElement createElement() => StreamProviderElement(this); /// {@macro riverpod.overridewith} + @mustBeOverridden Override overrideWith(Create, StreamProviderRef> create) { return ProviderOverride( origin: this, @@ -142,7 +143,10 @@ class StreamProvider extends _StreamProviderBase class StreamProviderElement extends ProviderElementBase> with FutureHandlerProviderElementMixin implements StreamProviderRef { - StreamProviderElement._(_StreamProviderBase super._provider); + /// The element of [StreamProvider]. + @internal + // ignore: library_private_types_in_public_api + StreamProviderElement(_StreamProviderBase super._provider); final _streamNotifier = ProxyElementValueNotifier>(); final StreamController _streamController = StreamController.broadcast(); diff --git a/packages/riverpod_annotation/lib/riverpod_annotation.dart b/packages/riverpod_annotation/lib/riverpod_annotation.dart index acfd987ac..05813b496 100644 --- a/packages/riverpod_annotation/lib/riverpod_annotation.dart +++ b/packages/riverpod_annotation/lib/riverpod_annotation.dart @@ -6,6 +6,9 @@ export 'package:riverpod/src/internals.dart' // General stuff Family, ProviderOrFamily, + Override, + // ignore: invalid_use_of_internal_member, Used by notifiers for overriding overrideWith + ProviderOverride, // Provider Provider, diff --git a/packages/riverpod_generator/CHANGELOG.md b/packages/riverpod_generator/CHANGELOG.md index 2991bcc18..2ba9560a6 100644 --- a/packages/riverpod_generator/CHANGELOG.md +++ b/packages/riverpod_generator/CHANGELOG.md @@ -1,3 +1,20 @@ +## Unreleased minor + +- The "ref" object now contains the provider parameters too. + This enabled `provider.overrideWith` to use the provider arguments: + ```dart + @riverpod + int example(ExampleRef ref, {int? id}) { /* */ } + // ... + exampleProvider.overrideWith( + (ref) { + print(ref.id); + } + ) + ``` +- Fix all `provider.overrideWith` causing a cast error if the notifier + receives arguments. + ## 2.2.6 - 2023-08-16 - Support both analyzer 5.12.0 and analyzer 6.0.0 diff --git a/packages/riverpod_generator/integration/build_yaml/lib/main.g.dart b/packages/riverpod_generator/integration/build_yaml/lib/main.g.dart index 06ff428d2..7a7a649ad 100644 --- a/packages/riverpod_generator/integration/build_yaml/lib/main.g.dart +++ b/packages/riverpod_generator/integration/build_yaml/lib/main.g.dart @@ -71,8 +71,6 @@ class _SystemHash { } } -typedef Count2Ref = AutoDisposeProviderRef; - /// See also [count2]. @ProviderFor(count2) const count2ProviderFamily = Count2Family(); @@ -119,10 +117,10 @@ class Count2Family extends Family { class Count2Provider extends AutoDisposeProvider { /// See also [count2]. Count2Provider( - this.a, - ) : super.internal( + int a, + ) : this._internal( (ref) => count2( - ref, + ref as Count2Ref, a, ), from: count2ProviderFamily, @@ -133,10 +131,44 @@ class Count2Provider extends AutoDisposeProvider { : _$count2Hash, dependencies: Count2Family._dependencies, allTransitiveDependencies: Count2Family._allTransitiveDependencies, + a: a, ); + Count2Provider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.a, + }) : super.internal(); + final int a; + @override + Override overrideWith( + int Function(Count2Ref provider) create, + ) { + return ProviderOverride( + origin: this, + override: Count2Provider._internal( + (ref) => create(ref as Count2Ref), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + a: a, + ), + ); + } + + @override + AutoDisposeProviderElement createElement() { + return _Count2ProviderElement(this); + } + @override bool operator ==(Object other) { return other is Count2Provider && other.a == a; @@ -151,8 +183,20 @@ class Count2Provider extends AutoDisposeProvider { } } +mixin Count2Ref on AutoDisposeProviderRef { + /// The parameter `a` of this provider. + int get a; +} + +class _Count2ProviderElement extends AutoDisposeProviderElement + with Count2Ref { + _Count2ProviderElement(super.provider); + + @override + int get a => (origin as Count2Provider).a; +} + String _$countFuture2Hash() => r'096675b70a267f5d7c62ac7d3e7dd231ef529034'; -typedef CountFuture2Ref = AutoDisposeFutureProviderRef; /// See also [countFuture2]. @ProviderFor(countFuture2) @@ -200,10 +244,10 @@ class CountFuture2Family extends Family> { class CountFuture2Provider extends AutoDisposeFutureProvider { /// See also [countFuture2]. CountFuture2Provider( - this.a, - ) : super.internal( + int a, + ) : this._internal( (ref) => countFuture2( - ref, + ref as CountFuture2Ref, a, ), from: countFuture2ProviderFamily, @@ -215,10 +259,44 @@ class CountFuture2Provider extends AutoDisposeFutureProvider { dependencies: CountFuture2Family._dependencies, allTransitiveDependencies: CountFuture2Family._allTransitiveDependencies, + a: a, ); + CountFuture2Provider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.a, + }) : super.internal(); + final int a; + @override + Override overrideWith( + FutureOr Function(CountFuture2Ref provider) create, + ) { + return ProviderOverride( + origin: this, + override: CountFuture2Provider._internal( + (ref) => create(ref as CountFuture2Ref), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + a: a, + ), + ); + } + + @override + AutoDisposeFutureProviderElement createElement() { + return _CountFuture2ProviderElement(this); + } + @override bool operator ==(Object other) { return other is CountFuture2Provider && other.a == a; @@ -233,8 +311,20 @@ class CountFuture2Provider extends AutoDisposeFutureProvider { } } +mixin CountFuture2Ref on AutoDisposeFutureProviderRef { + /// The parameter `a` of this provider. + int get a; +} + +class _CountFuture2ProviderElement extends AutoDisposeFutureProviderElement + with CountFuture2Ref { + _CountFuture2ProviderElement(super.provider); + + @override + int get a => (origin as CountFuture2Provider).a; +} + String _$countStream2Hash() => r'051264dd685ebc0a57e454bb676957c93cb4ae20'; -typedef CountStream2Ref = AutoDisposeStreamProviderRef; /// See also [countStream2]. @ProviderFor(countStream2) @@ -282,10 +372,10 @@ class CountStream2Family extends Family> { class CountStream2Provider extends AutoDisposeStreamProvider { /// See also [countStream2]. CountStream2Provider( - this.a, - ) : super.internal( + int a, + ) : this._internal( (ref) => countStream2( - ref, + ref as CountStream2Ref, a, ), from: countStream2ProviderFamily, @@ -297,10 +387,44 @@ class CountStream2Provider extends AutoDisposeStreamProvider { dependencies: CountStream2Family._dependencies, allTransitiveDependencies: CountStream2Family._allTransitiveDependencies, + a: a, ); + CountStream2Provider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.a, + }) : super.internal(); + final int a; + @override + Override overrideWith( + Stream Function(CountStream2Ref provider) create, + ) { + return ProviderOverride( + origin: this, + override: CountStream2Provider._internal( + (ref) => create(ref as CountStream2Ref), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + a: a, + ), + ); + } + + @override + AutoDisposeStreamProviderElement createElement() { + return _CountStream2ProviderElement(this); + } + @override bool operator ==(Object other) { return other is CountStream2Provider && other.a == a; @@ -315,6 +439,19 @@ class CountStream2Provider extends AutoDisposeStreamProvider { } } +mixin CountStream2Ref on AutoDisposeStreamProviderRef { + /// The parameter `a` of this provider. + int get a; +} + +class _CountStream2ProviderElement extends AutoDisposeStreamProviderElement + with CountStream2Ref { + _CountStream2ProviderElement(super.provider); + + @override + int get a => (origin as CountStream2Provider).a; +} + String _$countNotifierHash() => r'a8dd7a66ee0002b8af657245c4affaa206fd99ec'; /// See also [CountNotifier]. @@ -422,8 +559,8 @@ class CountNotifier2Provider extends AutoDisposeNotifierProviderImpl { /// See also [CountNotifier2]. CountNotifier2Provider( - this.a, - ) : super.internal( + int a, + ) : this._internal( () => CountNotifier2()..a = a, from: countNotifier2ProviderFamily, name: r'countNotifier2ProviderFamily', @@ -434,10 +571,51 @@ class CountNotifier2Provider dependencies: CountNotifier2Family._dependencies, allTransitiveDependencies: CountNotifier2Family._allTransitiveDependencies, + a: a, ); + CountNotifier2Provider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.a, + }) : super.internal(); + final int a; + @override + int runNotifierBuild( + covariant CountNotifier2 notifier, + ) { + return notifier.build( + a, + ); + } + + @override + Override overrideWith(CountNotifier2 Function() create) { + return ProviderOverride( + origin: this, + override: CountNotifier2Provider._internal( + () => create()..a = a, + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + a: a, + ), + ); + } + + @override + AutoDisposeNotifierProviderElement createElement() { + return _CountNotifier2ProviderElement(this); + } + @override bool operator ==(Object other) { return other is CountNotifier2Provider && other.a == a; @@ -450,15 +628,20 @@ class CountNotifier2Provider return _SystemHash.finish(hash); } +} + +mixin CountNotifier2Ref on AutoDisposeNotifierProviderRef { + /// The parameter `a` of this provider. + int get a; +} + +class _CountNotifier2ProviderElement + extends AutoDisposeNotifierProviderElement + with CountNotifier2Ref { + _CountNotifier2ProviderElement(super.provider); @override - int runNotifierBuild( - covariant CountNotifier2 notifier, - ) { - return notifier.build( - a, - ); - } + int get a => (origin as CountNotifier2Provider).a; } String _$countAsyncNotifier2Hash() => @@ -520,8 +703,8 @@ class CountAsyncNotifier2Provider extends AutoDisposeAsyncNotifierProviderImpl { /// See also [CountAsyncNotifier2]. CountAsyncNotifier2Provider( - this.a, - ) : super.internal( + int a, + ) : this._internal( () => CountAsyncNotifier2()..a = a, from: countAsyncNotifier2ProviderFamily, name: r'countAsyncNotifier2ProviderFamily', @@ -532,10 +715,52 @@ class CountAsyncNotifier2Provider dependencies: CountAsyncNotifier2Family._dependencies, allTransitiveDependencies: CountAsyncNotifier2Family._allTransitiveDependencies, + a: a, ); + CountAsyncNotifier2Provider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.a, + }) : super.internal(); + final int a; + @override + FutureOr runNotifierBuild( + covariant CountAsyncNotifier2 notifier, + ) { + return notifier.build( + a, + ); + } + + @override + Override overrideWith(CountAsyncNotifier2 Function() create) { + return ProviderOverride( + origin: this, + override: CountAsyncNotifier2Provider._internal( + () => create()..a = a, + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + a: a, + ), + ); + } + + @override + AutoDisposeAsyncNotifierProviderElement + createElement() { + return _CountAsyncNotifier2ProviderElement(this); + } + @override bool operator ==(Object other) { return other is CountAsyncNotifier2Provider && other.a == a; @@ -548,15 +773,20 @@ class CountAsyncNotifier2Provider return _SystemHash.finish(hash); } +} + +mixin CountAsyncNotifier2Ref on AutoDisposeAsyncNotifierProviderRef { + /// The parameter `a` of this provider. + int get a; +} + +class _CountAsyncNotifier2ProviderElement + extends AutoDisposeAsyncNotifierProviderElement + with CountAsyncNotifier2Ref { + _CountAsyncNotifier2ProviderElement(super.provider); @override - FutureOr runNotifierBuild( - covariant CountAsyncNotifier2 notifier, - ) { - return notifier.build( - a, - ); - } + int get a => (origin as CountAsyncNotifier2Provider).a; } String _$countStreamNotifier2Hash() => @@ -618,8 +848,8 @@ class CountStreamNotifier2Provider extends AutoDisposeStreamNotifierProviderImpl { /// See also [CountStreamNotifier2]. CountStreamNotifier2Provider( - this.a, - ) : super.internal( + int a, + ) : this._internal( () => CountStreamNotifier2()..a = a, from: countStreamNotifier2ProviderFamily, name: r'countStreamNotifier2ProviderFamily', @@ -630,10 +860,52 @@ class CountStreamNotifier2Provider dependencies: CountStreamNotifier2Family._dependencies, allTransitiveDependencies: CountStreamNotifier2Family._allTransitiveDependencies, + a: a, ); + CountStreamNotifier2Provider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.a, + }) : super.internal(); + final int a; + @override + Stream runNotifierBuild( + covariant CountStreamNotifier2 notifier, + ) { + return notifier.build( + a, + ); + } + + @override + Override overrideWith(CountStreamNotifier2 Function() create) { + return ProviderOverride( + origin: this, + override: CountStreamNotifier2Provider._internal( + () => create()..a = a, + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + a: a, + ), + ); + } + + @override + AutoDisposeStreamNotifierProviderElement + createElement() { + return _CountStreamNotifier2ProviderElement(this); + } + @override bool operator ==(Object other) { return other is CountStreamNotifier2Provider && other.a == a; @@ -646,15 +918,20 @@ class CountStreamNotifier2Provider return _SystemHash.finish(hash); } +} + +mixin CountStreamNotifier2Ref on AutoDisposeStreamNotifierProviderRef { + /// The parameter `a` of this provider. + int get a; +} + +class _CountStreamNotifier2ProviderElement + extends AutoDisposeStreamNotifierProviderElement + with CountStreamNotifier2Ref { + _CountStreamNotifier2ProviderElement(super.provider); @override - Stream runNotifierBuild( - covariant CountStreamNotifier2 notifier, - ) { - return notifier.build( - a, - ); - } + int get a => (origin as CountStreamNotifier2Provider).a; } // ignore_for_file: type=lint // ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member diff --git a/packages/riverpod_generator/lib/src/riverpod_generator.dart b/packages/riverpod_generator/lib/src/riverpod_generator.dart index 3d8943065..538e367b6 100644 --- a/packages/riverpod_generator/lib/src/riverpod_generator.dart +++ b/packages/riverpod_generator/lib/src/riverpod_generator.dart @@ -181,8 +181,6 @@ class _SystemHash { final hashFn = _hashFnIdentifier(hashFunctionName); buffer.write(_hashFn(provider, hashFunctionName)); - final refName = '${provider.providerElement.name.titled}Ref'; - // Using >1 as functional providers always have at least one parameter: ref // So a provider is a "family" only if it has parameters besides the ref. if (parameters.length > 1) { @@ -190,10 +188,10 @@ class _SystemHash { FamilyTemplate.functional( provider, options: options, - refName: refName, hashFn: hashFn, ).run(buffer); } else { + final refName = '${provider.providerElement.name.titled}Ref'; FunctionalProviderTemplate( provider, refName: refName, diff --git a/packages/riverpod_generator/lib/src/templates/family.dart b/packages/riverpod_generator/lib/src/templates/family.dart index 438c04efe..843dd7c84 100644 --- a/packages/riverpod_generator/lib/src/templates/family.dart +++ b/packages/riverpod_generator/lib/src/templates/family.dart @@ -21,6 +21,8 @@ class FamilyTemplate extends Template { required this.options, required this.parameters, required this.providerType, + required this.refType, + required this.elementType, required this.providerGenerics, required this.providerCreate, required this.parametersPassThrough, @@ -39,7 +41,6 @@ class FamilyTemplate extends Template { factory FamilyTemplate.functional( FunctionalProviderDeclaration provider, { - required String refName, required String hashFn, required BuildYamlOptions options, }) { @@ -49,13 +50,23 @@ class FamilyTemplate extends Template { } var providerType = '${leading}Provider'; + var refType = '${leading}ProviderRef'; + var elementType = '${leading}ProviderElement'; + var createdType = provider.createdType.toString(); final returnType = provider.createdType; if (!returnType.isRaw) { if (returnType.isDartAsyncFutureOr || returnType.isDartAsyncFuture) { providerType = '${leading}FutureProvider'; + refType = '${leading}FutureProviderRef'; + elementType = '${leading}FutureProviderElement'; + // Always use FutureOr in overrideWith as return value + // or otherwise we get a compilation error. + createdType = 'FutureOr<${provider.valueType}>'; } else if (returnType.isDartAsyncStream) { providerType = '${leading}StreamProvider'; + refType = '${leading}StreamProviderRef'; + elementType = '${leading}StreamProviderElement'; } } @@ -74,12 +85,32 @@ class FamilyTemplate extends Template { options: options, parameters: parameters, hashFn: hashFn, + elementType: elementType, + refType: refType, providerGenerics: '<${provider.valueType}>', - providerCreate: '(ref) => ${provider.name}(ref, $parametersPassThrough)', + providerCreate: + '(ref) => ${provider.name}(ref as ${provider._refImplName}, $parametersPassThrough)', providerType: providerType, parametersPassThrough: parametersPassThrough, - other: ''' -typedef $refName = ${providerType}Ref<${provider.valueType}>; + providerOther: ''' + + @override + Override overrideWith( + $createdType Function(${provider._refImplName} provider) create, + ) { + return ProviderOverride( + origin: this, + override: ${provider._providerImplName}._internal( + (ref) => create(ref as ${provider._refImplName}), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, +${parameters.map((e) => ' ${e.name}: ${e.name},\n').join()} + ), + ); + } ''', ); } @@ -96,16 +127,22 @@ typedef $refName = ${providerType}Ref<${provider.valueType}>; } var providerType = '${leading}NotifierProviderImpl'; + var refType = '${leading}NotifierProviderRef'; var notifierBaseType = 'Buildless${leading}Notifier'; + var elementType = '${leading}NotifierProviderElement'; final returnType = provider.createdType; if (!returnType.isRaw) { if (returnType.isDartAsyncFutureOr || returnType.isDartAsyncFuture) { providerType = '${leading}AsyncNotifierProviderImpl'; + refType = '${leading}AsyncNotifierProviderRef'; notifierBaseType = 'Buildless${leading}AsyncNotifier'; + elementType = '${leading}AsyncNotifierProviderElement'; } else if (returnType.isDartAsyncStream) { providerType = '${leading}StreamNotifierProviderImpl'; + refType = '${leading}StreamNotifierProviderRef'; notifierBaseType = 'Buildless${leading}StreamNotifier'; + elementType = '${leading}StreamNotifierProviderElement'; } } @@ -125,6 +162,8 @@ typedef $refName = ${providerType}Ref<${provider.valueType}>; options: options, parameters: parameters, hashFn: hashFn, + elementType: elementType, + refType: refType, providerGenerics: '<${provider.name}, ${provider.valueType}>', providerType: providerType, providerCreate: '() => ${provider.name}()$cascadePropertyInit', @@ -143,6 +182,22 @@ abstract class $notifierTypedefName extends $notifierBaseType<${provider.valueTy ) { return notifier.build($parametersPassThrough); } + + @override + Override overrideWith(${provider.name} Function() create) { + return ProviderOverride( + origin: this, + override: ${provider._providerImplName}._internal( + () => create()$cascadePropertyInit, + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, +${parameters.map((e) => ' ${e.name}: ${e.name},\n').join()} + ), + ); + } ''', ); } @@ -150,6 +205,8 @@ abstract class $notifierTypedefName extends $notifierBaseType<${provider.valueTy final GeneratorProviderDeclaration provider; final List parameters; final BuildYamlOptions options; + final String refType; + final String elementType; final String providerType; final String providerGenerics; final String providerCreate; @@ -160,15 +217,12 @@ abstract class $notifierTypedefName extends $notifierBaseType<${provider.valueTy @override void run(StringBuffer buffer) { - final providerTypeNameImpl = - '${provider.providerElement.name.titled}Provider'; + final providerTypeNameImpl = provider._providerImplName; + final refNameImpl = provider._refImplName; + final elementNameImpl = '_${providerTypeNameImpl.public}Element'; final familyName = '${provider.providerElement.name.titled}Family'; final parameterDefinition = buildParamDefinitionQuery(parameters); - final thisParameterDefinition = buildParamDefinitionQuery( - parameters, - asThisParameter: true, - ); final parameterProviderPassThrough = buildParamInvocationQuery({ for (final parameter in parameters) parameter: 'provider.${parameter.name}', @@ -224,17 +278,40 @@ class $familyName extends Family<${provider.exposedType}> { $docs class $providerTypeNameImpl extends $providerType$providerGenerics { $docs - $providerTypeNameImpl($thisParameterDefinition) : super.internal( + $providerTypeNameImpl($parameterDefinition) : this._internal( $providerCreate, from: $providerName, name: r'$providerName', debugGetCreateSourceHash: $hashFn, dependencies: $familyName._dependencies, allTransitiveDependencies: $familyName._allTransitiveDependencies, + ${parameters.map((e) => '${e.name}: ${e.name},\n').join()} ); + $providerTypeNameImpl._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + ${buildParamDefinitionQuery( + parameters, + asThisParameter: true, + writeBrackets: false, + asRequiredNamed: true, + )} + }) : super.internal(); + ${parameters.map((e) => 'final ${e.type.getDisplayString(withNullability: true)} ${e.name};').join()} +$providerOther + + @override + $elementType$providerGenerics createElement() { + return $elementNameImpl(this); + } + @override bool operator ==(Object other) { return ${[ @@ -250,8 +327,27 @@ ${parameters.map((e) => 'hash = _SystemHash.combine(hash, ${e.name}.hashCode);') return _SystemHash.finish(hash); } -$providerOther +} + +mixin $refNameImpl on $refType<${provider.valueType}> { + ${parameters.map((e) { + return ''' +/// The parameter `${e.name}` of this provider. +${e.type} get ${e.name};'''; + }).join()} +} + +class $elementNameImpl extends $elementType$providerGenerics with $refNameImpl { + $elementNameImpl(super.provider); + +${parameters.map((e) => '@override ${e.type} get ${e.name} => (origin as $providerTypeNameImpl).${e.name};').join()} } '''); } } + +extension on GeneratorProviderDeclaration { + String get _providerImplName => '${providerElement.name.titled}Provider'; + + String get _refImplName => '${providerElement.name.titled}Ref'; +} diff --git a/packages/riverpod_generator/lib/src/templates/parameters.dart b/packages/riverpod_generator/lib/src/templates/parameters.dart index a4843b306..55a302b8a 100644 --- a/packages/riverpod_generator/lib/src/templates/parameters.dart +++ b/packages/riverpod_generator/lib/src/templates/parameters.dart @@ -3,19 +3,33 @@ import 'package:analyzer/dart/element/element.dart'; String buildParamDefinitionQuery( List parameters, { bool asThisParameter = false, + bool asSuperParameter = false, + bool writeBrackets = true, + bool asRequiredNamed = false, }) { - final requiredPositionals = - parameters.where((element) => element.isRequiredPositional); - final optionalPositionals = - parameters.where((element) => element.isOptionalPositional).toList(); - final named = parameters.where((element) => element.isNamed).toList(); + assert( + !asThisParameter || !asSuperParameter, + 'Cannot enable both asThisParameter and asSuperParameter', + ); + + final requiredPositionals = parameters + .where((element) => element.isRequiredPositional && !asRequiredNamed) + .toList(); + final optionalPositionals = parameters + .where((element) => element.isOptionalPositional && !asRequiredNamed) + .toList(); + final named = parameters + .where((element) => element.isNamed || asRequiredNamed) + .toList(); final buffer = StringBuffer(); String encodeParameter(ParameterElement e) { - final leading = e.isRequiredNamed ? 'required ' : ''; - final trailing = - e.defaultValueCode != null ? '= ${e.defaultValueCode}' : ''; + final leading = e.isRequiredNamed || asRequiredNamed ? 'required ' : ''; + final trailing = e.defaultValueCode != null && !asRequiredNamed + ? '= ${e.defaultValueCode}' + : ''; if (asThisParameter) return '${leading}this.${e.name}$trailing'; + if (asSuperParameter) return '${leading}super.${e.name}$trailing'; return '$leading${e.type} ${e.name}$trailing'; } @@ -23,18 +37,16 @@ String buildParamDefinitionQuery( requiredPositionals.map(encodeParameter).expand((e) => [e, ',']), ); if (optionalPositionals.isNotEmpty) { - buffer - ..write('[') - ..writeAll( - optionalPositionals.map(encodeParameter).expand((e) => [e, ',']), - ) - ..write(']'); + if (writeBrackets) buffer.write('['); + buffer.writeAll( + optionalPositionals.map(encodeParameter).expand((e) => [e, ',']), + ); + if (writeBrackets) buffer.write(']'); } if (named.isNotEmpty) { - buffer - ..write('{') - ..writeAll(named.map(encodeParameter).expand((e) => [e, ','])) - ..write('}'); + if (writeBrackets) buffer.write('{'); + buffer.writeAll(named.map(encodeParameter).expand((e) => [e, ','])); + if (writeBrackets) buffer.write('}'); } return buffer.toString(); diff --git a/packages/riverpod_generator/test/async_notifier_test.dart b/packages/riverpod_generator/test/async_notifier_test.dart index 8882d233f..c8664e5d2 100644 --- a/packages/riverpod_generator/test/async_notifier_test.dart +++ b/packages/riverpod_generator/test/async_notifier_test.dart @@ -27,9 +27,44 @@ void main() { expect(familyClassProvider(42, third: .42).name, 'familyClassProvider'); }); + test('Supports overriding non-family notifiers', () { + final container = createContainer( + overrides: [ + publicClassProvider.overrideWith(() => PublicClass('Hello world')), + ], + ); + + final notifier = container.read(publicClassProvider.notifier); + expect(notifier.param, 'Hello world'); + + expect(notifier.ref, isNotNull); + expect(notifier.state, isNotNull); + }); + + test('Supports overriding family notifiers', () { + final container = createContainer( + overrides: [ + familyClassProvider(42, third: .42) + .overrideWith(() => FamilyClass('Hello world')), + ], + ); + + final notifier = + container.read(familyClassProvider(42, third: .42).notifier); + expect(notifier.param, 'Hello world'); + expect(notifier.first, 42); + expect(notifier.second, null); + expect(notifier.third, .42); + expect(notifier.fourth, true); + expect(notifier.fifth, null); + + expect(notifier.ref, isNotNull); + expect(notifier.state, isNotNull); + }); + test( 'Creates a NotifierProvider.family if @riverpod is used on a synchronous function with parameters', - () { + () async { final container = createContainer(); const FamilyClassFamily family = familyClassProvider; @@ -74,7 +109,7 @@ void main() { second: 'x42', third: .42, fourth: false, - fifth: ['x42'], + fifth: const ['x42'], ); // ignore: invalid_use_of_internal_member final AutoDisposeAsyncNotifierProviderImpl @@ -84,7 +119,19 @@ void main() { expect(provider.second, 'x42'); expect(provider.third, .42); expect(provider.fourth, false); - expect(provider.fifth, ['x42']); + expect(provider.fifth, same(const ['x42'])); + + final sub = container.listen( + familyClassProvider( + 42, + second: 'x42', + third: .42, + fourth: false, + fifth: const ['x42'], + ).future, + (previous, next) {}, + ); + await sub.read(); final AsyncValue result = container.read( familyClassProvider( @@ -92,7 +139,7 @@ void main() { second: 'x42', third: .42, fourth: false, - fifth: ['x42'], + fifth: const ['x42'], ), ); diff --git a/packages/riverpod_generator/test/async_test.dart b/packages/riverpod_generator/test/async_test.dart index d1d38e49b..cfb2bf807 100644 --- a/packages/riverpod_generator/test/async_test.dart +++ b/packages/riverpod_generator/test/async_test.dart @@ -26,9 +26,36 @@ void main() { expect(familyProvider(42, third: .42).name, 'familyProvider'); }); + test('Supports overriding non-family providers', () async { + final container = createContainer( + overrides: [ + publicProvider.overrideWith((ref) => Future.value('Hello world')), + ], + ); + + final result = container.read(publicProvider.future); + expect(await result, 'Hello world'); + }); + + test('Supports overriding family providers', () async { + final container = createContainer( + overrides: [ + familyProvider(42, third: .42).overrideWith( + (ref) => Future.value( + 'Hello world ${ref.first} ${ref.second} ' + '${ref.third} ${ref.fourth} ${ref.fifth}', + ), + ), + ], + ); + + final result = container.read(familyProvider(42, third: .42).future); + expect(await result, 'Hello world 42 null 0.42 true null'); + }); + test( 'Creates a Provider.family if @riverpod is used on a synchronous function with parameters', - () { + () async { final container = createContainer(); const FamilyFamily family = familyProvider; @@ -73,7 +100,7 @@ void main() { second: 'x42', third: .42, fourth: false, - fifth: ['x42'], + fifth: const ['x42'], ); final AutoDisposeFutureProvider futureProvider = provider; @@ -81,7 +108,19 @@ void main() { expect(provider.second, 'x42'); expect(provider.third, .42); expect(provider.fourth, false); - expect(provider.fifth, ['x42']); + expect(provider.fifth, same(const ['x42'])); + + final sub = container.listen( + familyProvider( + 42, + second: 'x42', + third: .42, + fourth: false, + fifth: const ['x42'], + ).future, + (previous, next) {}, + ); + await sub.read(); final AsyncValue result = container.read( familyProvider( @@ -89,7 +128,7 @@ void main() { second: 'x42', third: .42, fourth: false, - fifth: ['x42'], + fifth: const ['x42'], ), ); diff --git a/packages/riverpod_generator/test/integration/async.dart b/packages/riverpod_generator/test/integration/async.dart index 4ee104e96..26d314052 100644 --- a/packages/riverpod_generator/test/integration/async.dart +++ b/packages/riverpod_generator/test/integration/async.dart @@ -15,19 +15,28 @@ Future _private(_PrivateRef ref) async { } @riverpod -FutureOr family( +FutureOr familyOr(FamilyOrRef ref, int first) { + return '(first: $first)'; +} + +@riverpod +Future family( FamilyRef ref, int first, { String? second, required double third, bool fourth = true, List? fifth, -}) { +}) async { return '(first: $first, second: $second, third: $third, fourth: $fourth, fifth: $fifth)'; } @riverpod class PublicClass extends _$PublicClass { + PublicClass([this.param]); + + final Object? param; + @override FutureOr build() { return 'Hello world'; @@ -44,16 +53,28 @@ class _PrivateClass extends _$PrivateClass { } } +@riverpod +class FamilyOrClass extends _$FamilyOrClass { + @override + FutureOr build(int first) { + return '(first: $first)'; + } +} + @riverpod class FamilyClass extends _$FamilyClass { + FamilyClass([this.param]); + + final Object? param; + @override - FutureOr build( + Future build( int first, { String? second, required double third, bool fourth = true, List? fifth, - }) { + }) async { return '(first: $first, second: $second, third: $third, fourth: $fourth, fifth: $fifth)'; } } diff --git a/packages/riverpod_generator/test/integration/async.g.dart b/packages/riverpod_generator/test/integration/async.g.dart index 00d20e7ed..53a68fce7 100644 --- a/packages/riverpod_generator/test/integration/async.g.dart +++ b/packages/riverpod_generator/test/integration/async.g.dart @@ -34,7 +34,7 @@ final _privateProvider = AutoDisposeFutureProvider.internal( ); typedef _PrivateRef = AutoDisposeFutureProviderRef; -String _$familyHash() => r'f46defb7b007c76254058e9e8bc868260bcfe0f1'; +String _$familyOrHash() => r'1c3217e296b0ce52c07c18769d1fffb95850f482'; /// Copied from Dart SDK class _SystemHash { @@ -57,7 +57,132 @@ class _SystemHash { } } -typedef FamilyRef = AutoDisposeFutureProviderRef; +/// See also [familyOr]. +@ProviderFor(familyOr) +const familyOrProvider = FamilyOrFamily(); + +/// See also [familyOr]. +class FamilyOrFamily extends Family> { + /// See also [familyOr]. + const FamilyOrFamily(); + + /// See also [familyOr]. + FamilyOrProvider call( + int first, + ) { + return FamilyOrProvider( + first, + ); + } + + @override + FamilyOrProvider getProviderOverride( + covariant FamilyOrProvider provider, + ) { + return call( + provider.first, + ); + } + + static const Iterable? _dependencies = null; + + @override + Iterable? get dependencies => _dependencies; + + static const Iterable? _allTransitiveDependencies = null; + + @override + Iterable? get allTransitiveDependencies => + _allTransitiveDependencies; + + @override + String? get name => r'familyOrProvider'; +} + +/// See also [familyOr]. +class FamilyOrProvider extends AutoDisposeFutureProvider { + /// See also [familyOr]. + FamilyOrProvider( + int first, + ) : this._internal( + (ref) => familyOr( + ref as FamilyOrRef, + first, + ), + from: familyOrProvider, + name: r'familyOrProvider', + debugGetCreateSourceHash: + const bool.fromEnvironment('dart.vm.product') + ? null + : _$familyOrHash, + dependencies: FamilyOrFamily._dependencies, + allTransitiveDependencies: FamilyOrFamily._allTransitiveDependencies, + first: first, + ); + + FamilyOrProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.first, + }) : super.internal(); + + final int first; + + @override + Override overrideWith( + FutureOr Function(FamilyOrRef provider) create, + ) { + return ProviderOverride( + origin: this, + override: FamilyOrProvider._internal( + (ref) => create(ref as FamilyOrRef), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + first: first, + ), + ); + } + + @override + AutoDisposeFutureProviderElement createElement() { + return _FamilyOrProviderElement(this); + } + + @override + bool operator ==(Object other) { + return other is FamilyOrProvider && other.first == first; + } + + @override + int get hashCode { + var hash = _SystemHash.combine(0, runtimeType.hashCode); + hash = _SystemHash.combine(hash, first.hashCode); + + return _SystemHash.finish(hash); + } +} + +mixin FamilyOrRef on AutoDisposeFutureProviderRef { + /// The parameter `first` of this provider. + int get first; +} + +class _FamilyOrProviderElement extends AutoDisposeFutureProviderElement + with FamilyOrRef { + _FamilyOrProviderElement(super.provider); + + @override + int get first => (origin as FamilyOrProvider).first; +} + +String _$familyHash() => r'eb6fad35a94d4238b621c2100253ee2c700bee77'; /// See also [family]. @ProviderFor(family) @@ -117,14 +242,14 @@ class FamilyFamily extends Family> { class FamilyProvider extends AutoDisposeFutureProvider { /// See also [family]. FamilyProvider( - this.first, { - this.second, - required this.third, - this.fourth = true, - this.fifth, - }) : super.internal( + int first, { + String? second, + required double third, + bool fourth = true, + List? fifth, + }) : this._internal( (ref) => family( - ref, + ref as FamilyRef, first, second: second, third: third, @@ -139,14 +264,60 @@ class FamilyProvider extends AutoDisposeFutureProvider { : _$familyHash, dependencies: FamilyFamily._dependencies, allTransitiveDependencies: FamilyFamily._allTransitiveDependencies, + first: first, + second: second, + third: third, + fourth: fourth, + fifth: fifth, ); + FamilyProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.first, + required this.second, + required this.third, + required this.fourth, + required this.fifth, + }) : super.internal(); + final int first; final String? second; final double third; final bool fourth; final List? fifth; + @override + Override overrideWith( + FutureOr Function(FamilyRef provider) create, + ) { + return ProviderOverride( + origin: this, + override: FamilyProvider._internal( + (ref) => create(ref as FamilyRef), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + first: first, + second: second, + third: third, + fourth: fourth, + fifth: fifth, + ), + ); + } + + @override + AutoDisposeFutureProviderElement createElement() { + return _FamilyProviderElement(this); + } + @override bool operator ==(Object other) { return other is FamilyProvider && @@ -170,7 +341,40 @@ class FamilyProvider extends AutoDisposeFutureProvider { } } -String _$publicClassHash() => r'98f7b5a2478814264c0a70d066ecabfddc58c577'; +mixin FamilyRef on AutoDisposeFutureProviderRef { + /// The parameter `first` of this provider. + int get first; + + /// The parameter `second` of this provider. + String? get second; + + /// The parameter `third` of this provider. + double get third; + + /// The parameter `fourth` of this provider. + bool get fourth; + + /// The parameter `fifth` of this provider. + List? get fifth; +} + +class _FamilyProviderElement extends AutoDisposeFutureProviderElement + with FamilyRef { + _FamilyProviderElement(super.provider); + + @override + int get first => (origin as FamilyProvider).first; + @override + String? get second => (origin as FamilyProvider).second; + @override + double get third => (origin as FamilyProvider).third; + @override + bool get fourth => (origin as FamilyProvider).fourth; + @override + List? get fifth => (origin as FamilyProvider).fifth; +} + +String _$publicClassHash() => r'e9bc69e44b72e8ed77d423524c0d74ad460d629d'; /// See also [PublicClass]. @ProviderFor(PublicClass) @@ -200,7 +404,151 @@ final _privateClassProvider = ); typedef _$PrivateClass = AutoDisposeAsyncNotifier; -String _$familyClassHash() => r'7b31f94e49dff1aa8b2f88d41b8a94e9a6434408'; +String _$familyOrClassHash() => r'b4882d4e79a03c63005d35eb7a021c9c4373a8d9'; + +abstract class _$FamilyOrClass + extends BuildlessAutoDisposeAsyncNotifier { + late final int first; + + FutureOr build( + int first, + ); +} + +/// See also [FamilyOrClass]. +@ProviderFor(FamilyOrClass) +const familyOrClassProvider = FamilyOrClassFamily(); + +/// See also [FamilyOrClass]. +class FamilyOrClassFamily extends Family> { + /// See also [FamilyOrClass]. + const FamilyOrClassFamily(); + + /// See also [FamilyOrClass]. + FamilyOrClassProvider call( + int first, + ) { + return FamilyOrClassProvider( + first, + ); + } + + @override + FamilyOrClassProvider getProviderOverride( + covariant FamilyOrClassProvider provider, + ) { + return call( + provider.first, + ); + } + + static const Iterable? _dependencies = null; + + @override + Iterable? get dependencies => _dependencies; + + static const Iterable? _allTransitiveDependencies = null; + + @override + Iterable? get allTransitiveDependencies => + _allTransitiveDependencies; + + @override + String? get name => r'familyOrClassProvider'; +} + +/// See also [FamilyOrClass]. +class FamilyOrClassProvider + extends AutoDisposeAsyncNotifierProviderImpl { + /// See also [FamilyOrClass]. + FamilyOrClassProvider( + int first, + ) : this._internal( + () => FamilyOrClass()..first = first, + from: familyOrClassProvider, + name: r'familyOrClassProvider', + debugGetCreateSourceHash: + const bool.fromEnvironment('dart.vm.product') + ? null + : _$familyOrClassHash, + dependencies: FamilyOrClassFamily._dependencies, + allTransitiveDependencies: + FamilyOrClassFamily._allTransitiveDependencies, + first: first, + ); + + FamilyOrClassProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.first, + }) : super.internal(); + + final int first; + + @override + FutureOr runNotifierBuild( + covariant FamilyOrClass notifier, + ) { + return notifier.build( + first, + ); + } + + @override + Override overrideWith(FamilyOrClass Function() create) { + return ProviderOverride( + origin: this, + override: FamilyOrClassProvider._internal( + () => create()..first = first, + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + first: first, + ), + ); + } + + @override + AutoDisposeAsyncNotifierProviderElement + createElement() { + return _FamilyOrClassProviderElement(this); + } + + @override + bool operator ==(Object other) { + return other is FamilyOrClassProvider && other.first == first; + } + + @override + int get hashCode { + var hash = _SystemHash.combine(0, runtimeType.hashCode); + hash = _SystemHash.combine(hash, first.hashCode); + + return _SystemHash.finish(hash); + } +} + +mixin FamilyOrClassRef on AutoDisposeAsyncNotifierProviderRef { + /// The parameter `first` of this provider. + int get first; +} + +class _FamilyOrClassProviderElement + extends AutoDisposeAsyncNotifierProviderElement + with FamilyOrClassRef { + _FamilyOrClassProviderElement(super.provider); + + @override + int get first => (origin as FamilyOrClassProvider).first; +} + +String _$familyClassHash() => r'b7e3ca6091f12bbc99972e961acd885e05f42a15'; abstract class _$FamilyClass extends BuildlessAutoDisposeAsyncNotifier { late final int first; @@ -209,7 +557,7 @@ abstract class _$FamilyClass extends BuildlessAutoDisposeAsyncNotifier { late final bool fourth; late final List? fifth; - FutureOr build( + Future build( int first, { String? second, required double third, @@ -277,12 +625,12 @@ class FamilyClassProvider extends AutoDisposeAsyncNotifierProviderImpl { /// See also [FamilyClass]. FamilyClassProvider( - this.first, { - this.second, - required this.third, - this.fourth = true, - this.fifth, - }) : super.internal( + int first, { + String? second, + required double third, + bool fourth = true, + List? fifth, + }) : this._internal( () => FamilyClass() ..first = first ..second = second @@ -298,14 +646,76 @@ class FamilyClassProvider dependencies: FamilyClassFamily._dependencies, allTransitiveDependencies: FamilyClassFamily._allTransitiveDependencies, + first: first, + second: second, + third: third, + fourth: fourth, + fifth: fifth, ); + FamilyClassProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.first, + required this.second, + required this.third, + required this.fourth, + required this.fifth, + }) : super.internal(); + final int first; final String? second; final double third; final bool fourth; final List? fifth; + @override + Future runNotifierBuild( + covariant FamilyClass notifier, + ) { + return notifier.build( + first, + second: second, + third: third, + fourth: fourth, + fifth: fifth, + ); + } + + @override + Override overrideWith(FamilyClass Function() create) { + return ProviderOverride( + origin: this, + override: FamilyClassProvider._internal( + () => create() + ..first = first + ..second = second + ..third = third + ..fourth = fourth + ..fifth = fifth, + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + first: first, + second: second, + third: third, + fourth: fourth, + fifth: fifth, + ), + ); + } + + @override + AutoDisposeAsyncNotifierProviderElement createElement() { + return _FamilyClassProviderElement(this); + } + @override bool operator ==(Object other) { return other is FamilyClassProvider && @@ -327,19 +737,40 @@ class FamilyClassProvider return _SystemHash.finish(hash); } +} + +mixin FamilyClassRef on AutoDisposeAsyncNotifierProviderRef { + /// The parameter `first` of this provider. + int get first; + + /// The parameter `second` of this provider. + String? get second; + + /// The parameter `third` of this provider. + double get third; + + /// The parameter `fourth` of this provider. + bool get fourth; + + /// The parameter `fifth` of this provider. + List? get fifth; +} + +class _FamilyClassProviderElement + extends AutoDisposeAsyncNotifierProviderElement + with FamilyClassRef { + _FamilyClassProviderElement(super.provider); @override - FutureOr runNotifierBuild( - covariant FamilyClass notifier, - ) { - return notifier.build( - first, - second: second, - third: third, - fourth: fourth, - fifth: fifth, - ); - } + int get first => (origin as FamilyClassProvider).first; + @override + String? get second => (origin as FamilyClassProvider).second; + @override + double get third => (origin as FamilyClassProvider).third; + @override + bool get fourth => (origin as FamilyClassProvider).fourth; + @override + List? get fifth => (origin as FamilyClassProvider).fifth; } // ignore_for_file: type=lint // ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member diff --git a/packages/riverpod_generator/test/integration/dependencies.g.dart b/packages/riverpod_generator/test/integration/dependencies.g.dart index f410062f0..5f9a2aa63 100644 --- a/packages/riverpod_generator/test/integration/dependencies.g.dart +++ b/packages/riverpod_generator/test/integration/dependencies.g.dart @@ -43,8 +43,6 @@ class _SystemHash { } } -typedef FamilyRef = AutoDisposeProviderRef; - /// See also [family]. @ProviderFor(family) const familyProvider = FamilyFamily(); @@ -91,10 +89,10 @@ class FamilyFamily extends Family { class FamilyProvider extends AutoDisposeProvider { /// See also [family]. FamilyProvider( - this.id, - ) : super.internal( + int id, + ) : this._internal( (ref) => family( - ref, + ref as FamilyRef, id, ), from: familyProvider, @@ -105,10 +103,44 @@ class FamilyProvider extends AutoDisposeProvider { : _$familyHash, dependencies: FamilyFamily._dependencies, allTransitiveDependencies: FamilyFamily._allTransitiveDependencies, + id: id, ); + FamilyProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.id, + }) : super.internal(); + final int id; + @override + Override overrideWith( + int Function(FamilyRef provider) create, + ) { + return ProviderOverride( + origin: this, + override: FamilyProvider._internal( + (ref) => create(ref as FamilyRef), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + id: id, + ), + ); + } + + @override + AutoDisposeProviderElement createElement() { + return _FamilyProviderElement(this); + } + @override bool operator ==(Object other) { return other is FamilyProvider && other.id == id; @@ -123,6 +155,19 @@ class FamilyProvider extends AutoDisposeProvider { } } +mixin FamilyRef on AutoDisposeProviderRef { + /// The parameter `id` of this provider. + int get id; +} + +class _FamilyProviderElement extends AutoDisposeProviderElement + with FamilyRef { + _FamilyProviderElement(super.provider); + + @override + int get id => (origin as FamilyProvider).id; +} + String _$providerHash() => r'6c9184ef4c6a410a2132e1ecc13a2e646e936d37'; /// See also [provider]. @@ -223,7 +268,7 @@ final smallTransitiveDependencyCountProvider = typedef SmallTransitiveDependencyCountRef = AutoDisposeProviderRef; String _$emptyDependenciesFunctionalHash() => - r'73d68dcbd3ea09b593b84b4a4dae9eb6e72e3640'; + r'592bebd079450e2071fb12d68c3ae333d5c28359'; /// See also [emptyDependenciesFunctional]. @ProviderFor(emptyDependenciesFunctional) @@ -357,8 +402,8 @@ class Family2Family extends Family { class Family2Provider extends AutoDisposeNotifierProviderImpl { /// See also [Family2]. Family2Provider( - this.id, - ) : super.internal( + int id, + ) : this._internal( () => Family2()..id = id, from: family2Provider, name: r'family2Provider', @@ -368,10 +413,51 @@ class Family2Provider extends AutoDisposeNotifierProviderImpl { : _$family2Hash, dependencies: Family2Family._dependencies, allTransitiveDependencies: Family2Family._allTransitiveDependencies, + id: id, ); + Family2Provider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.id, + }) : super.internal(); + final int id; + @override + int runNotifierBuild( + covariant Family2 notifier, + ) { + return notifier.build( + id, + ); + } + + @override + Override overrideWith(Family2 Function() create) { + return ProviderOverride( + origin: this, + override: Family2Provider._internal( + () => create()..id = id, + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + id: id, + ), + ); + } + + @override + AutoDisposeNotifierProviderElement createElement() { + return _Family2ProviderElement(this); + } + @override bool operator ==(Object other) { return other is Family2Provider && other.id == id; @@ -384,15 +470,19 @@ class Family2Provider extends AutoDisposeNotifierProviderImpl { return _SystemHash.finish(hash); } +} + +mixin Family2Ref on AutoDisposeNotifierProviderRef { + /// The parameter `id` of this provider. + int get id; +} + +class _Family2ProviderElement + extends AutoDisposeNotifierProviderElement with Family2Ref { + _Family2ProviderElement(super.provider); @override - int runNotifierBuild( - covariant Family2 notifier, - ) { - return notifier.build( - id, - ); - } + int get id => (origin as Family2Provider).id; } String _$provider3Hash() => r'dfdd6dec6cfee543c73d99593ce98d68f4db385c'; @@ -495,8 +585,8 @@ class Provider4Provider extends AutoDisposeNotifierProviderImpl { /// See also [Provider4]. Provider4Provider( - this.id, - ) : super.internal( + int id, + ) : this._internal( () => Provider4()..id = id, from: provider4Provider, name: r'provider4Provider', @@ -506,10 +596,51 @@ class Provider4Provider : _$provider4Hash, dependencies: Provider4Family._dependencies, allTransitiveDependencies: Provider4Family._allTransitiveDependencies, + id: id, ); + Provider4Provider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.id, + }) : super.internal(); + final int id; + @override + int runNotifierBuild( + covariant Provider4 notifier, + ) { + return notifier.build( + id, + ); + } + + @override + Override overrideWith(Provider4 Function() create) { + return ProviderOverride( + origin: this, + override: Provider4Provider._internal( + () => create()..id = id, + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + id: id, + ), + ); + } + + @override + AutoDisposeNotifierProviderElement createElement() { + return _Provider4ProviderElement(this); + } + @override bool operator ==(Object other) { return other is Provider4Provider && other.id == id; @@ -522,15 +653,20 @@ class Provider4Provider return _SystemHash.finish(hash); } +} + +mixin Provider4Ref on AutoDisposeNotifierProviderRef { + /// The parameter `id` of this provider. + int get id; +} + +class _Provider4ProviderElement + extends AutoDisposeNotifierProviderElement + with Provider4Ref { + _Provider4ProviderElement(super.provider); @override - int runNotifierBuild( - covariant Provider4 notifier, - ) { - return notifier.build( - id, - ); - } + int get id => (origin as Provider4Provider).id; } String _$emptyDependenciesClassBasedHash() => diff --git a/packages/riverpod_generator/test/integration/dependencies2.g.dart b/packages/riverpod_generator/test/integration/dependencies2.g.dart index c81220c9f..3b89d9d50 100644 --- a/packages/riverpod_generator/test/integration/dependencies2.g.dart +++ b/packages/riverpod_generator/test/integration/dependencies2.g.dart @@ -57,8 +57,6 @@ class _SystemHash { } } -typedef FamilyWithDependencies2Ref = AutoDisposeProviderRef; - /// See also [familyWithDependencies2]. @ProviderFor(familyWithDependencies2) const familyWithDependencies2Provider = FamilyWithDependencies2Family(); @@ -117,10 +115,10 @@ class FamilyWithDependencies2Family extends Family { class FamilyWithDependencies2Provider extends AutoDisposeProvider { /// See also [familyWithDependencies2]. FamilyWithDependencies2Provider({ - this.id, - }) : super.internal( + int? id, + }) : this._internal( (ref) => familyWithDependencies2( - ref, + ref as FamilyWithDependencies2Ref, id: id, ), from: familyWithDependencies2Provider, @@ -132,10 +130,44 @@ class FamilyWithDependencies2Provider extends AutoDisposeProvider { dependencies: FamilyWithDependencies2Family._dependencies, allTransitiveDependencies: FamilyWithDependencies2Family._allTransitiveDependencies, + id: id, ); + FamilyWithDependencies2Provider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.id, + }) : super.internal(); + final int? id; + @override + Override overrideWith( + int Function(FamilyWithDependencies2Ref provider) create, + ) { + return ProviderOverride( + origin: this, + override: FamilyWithDependencies2Provider._internal( + (ref) => create(ref as FamilyWithDependencies2Ref), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + id: id, + ), + ); + } + + @override + AutoDisposeProviderElement createElement() { + return _FamilyWithDependencies2ProviderElement(this); + } + @override bool operator ==(Object other) { return other is FamilyWithDependencies2Provider && other.id == id; @@ -150,6 +182,19 @@ class FamilyWithDependencies2Provider extends AutoDisposeProvider { } } +mixin FamilyWithDependencies2Ref on AutoDisposeProviderRef { + /// The parameter `id` of this provider. + int? get id; +} + +class _FamilyWithDependencies2ProviderElement + extends AutoDisposeProviderElement with FamilyWithDependencies2Ref { + _FamilyWithDependencies2ProviderElement(super.provider); + + @override + int? get id => (origin as FamilyWithDependencies2Provider).id; +} + String _$private2Hash() => r'5e0fa14ff40fb444c027ed25150a42362db3ef19'; /// See also [_private2]. @@ -279,8 +324,8 @@ class NotifierFamilyWithDependenciesProvider int> { /// See also [NotifierFamilyWithDependencies]. NotifierFamilyWithDependenciesProvider({ - this.id, - }) : super.internal( + int? id, + }) : this._internal( () => NotifierFamilyWithDependencies()..id = id, from: notifierFamilyWithDependenciesProvider, name: r'notifierFamilyWithDependenciesProvider', @@ -291,10 +336,52 @@ class NotifierFamilyWithDependenciesProvider dependencies: NotifierFamilyWithDependenciesFamily._dependencies, allTransitiveDependencies: NotifierFamilyWithDependenciesFamily._allTransitiveDependencies, + id: id, ); + NotifierFamilyWithDependenciesProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.id, + }) : super.internal(); + final int? id; + @override + int runNotifierBuild( + covariant NotifierFamilyWithDependencies notifier, + ) { + return notifier.build( + id: id, + ); + } + + @override + Override overrideWith(NotifierFamilyWithDependencies Function() create) { + return ProviderOverride( + origin: this, + override: NotifierFamilyWithDependenciesProvider._internal( + () => create()..id = id, + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + id: id, + ), + ); + } + + @override + AutoDisposeNotifierProviderElement + createElement() { + return _NotifierFamilyWithDependenciesProviderElement(this); + } + @override bool operator ==(Object other) { return other is NotifierFamilyWithDependenciesProvider && other.id == id; @@ -307,15 +394,20 @@ class NotifierFamilyWithDependenciesProvider return _SystemHash.finish(hash); } +} + +mixin NotifierFamilyWithDependenciesRef on AutoDisposeNotifierProviderRef { + /// The parameter `id` of this provider. + int? get id; +} + +class _NotifierFamilyWithDependenciesProviderElement + extends AutoDisposeNotifierProviderElement with NotifierFamilyWithDependenciesRef { + _NotifierFamilyWithDependenciesProviderElement(super.provider); @override - int runNotifierBuild( - covariant NotifierFamilyWithDependencies notifier, - ) { - return notifier.build( - id: id, - ); - } + int? get id => (origin as NotifierFamilyWithDependenciesProvider).id; } // ignore_for_file: type=lint // ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member diff --git a/packages/riverpod_generator/test/integration/stream.dart b/packages/riverpod_generator/test/integration/stream.dart index 99071d3bf..7712da5c5 100644 --- a/packages/riverpod_generator/test/integration/stream.dart +++ b/packages/riverpod_generator/test/integration/stream.dart @@ -30,6 +30,10 @@ Stream family( @riverpod class PublicClass extends _$PublicClass { + PublicClass([this.param]); + + final Object? param; + @override Stream build() { return Stream.value('Hello world'); @@ -48,6 +52,10 @@ class _PrivateClass extends _$PrivateClass { @riverpod class FamilyClass extends _$FamilyClass { + FamilyClass([this.param]); + + final Object? param; + @override Stream build( int first, { diff --git a/packages/riverpod_generator/test/integration/stream.g.dart b/packages/riverpod_generator/test/integration/stream.g.dart index d306c80eb..58dac0d73 100644 --- a/packages/riverpod_generator/test/integration/stream.g.dart +++ b/packages/riverpod_generator/test/integration/stream.g.dart @@ -57,8 +57,6 @@ class _SystemHash { } } -typedef FamilyRef = AutoDisposeStreamProviderRef; - /// See also [family]. @ProviderFor(family) const familyProvider = FamilyFamily(); @@ -117,14 +115,14 @@ class FamilyFamily extends Family> { class FamilyProvider extends AutoDisposeStreamProvider { /// See also [family]. FamilyProvider( - this.first, { - this.second, - required this.third, - this.fourth = true, - this.fifth, - }) : super.internal( + int first, { + String? second, + required double third, + bool fourth = true, + List? fifth, + }) : this._internal( (ref) => family( - ref, + ref as FamilyRef, first, second: second, third: third, @@ -139,14 +137,60 @@ class FamilyProvider extends AutoDisposeStreamProvider { : _$familyHash, dependencies: FamilyFamily._dependencies, allTransitiveDependencies: FamilyFamily._allTransitiveDependencies, + first: first, + second: second, + third: third, + fourth: fourth, + fifth: fifth, ); + FamilyProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.first, + required this.second, + required this.third, + required this.fourth, + required this.fifth, + }) : super.internal(); + final int first; final String? second; final double third; final bool fourth; final List? fifth; + @override + Override overrideWith( + Stream Function(FamilyRef provider) create, + ) { + return ProviderOverride( + origin: this, + override: FamilyProvider._internal( + (ref) => create(ref as FamilyRef), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + first: first, + second: second, + third: third, + fourth: fourth, + fifth: fifth, + ), + ); + } + + @override + AutoDisposeStreamProviderElement createElement() { + return _FamilyProviderElement(this); + } + @override bool operator ==(Object other) { return other is FamilyProvider && @@ -170,7 +214,40 @@ class FamilyProvider extends AutoDisposeStreamProvider { } } -String _$publicClassHash() => r'a0b49ed7018eb64309ef147c2d058a45d6092b01'; +mixin FamilyRef on AutoDisposeStreamProviderRef { + /// The parameter `first` of this provider. + int get first; + + /// The parameter `second` of this provider. + String? get second; + + /// The parameter `third` of this provider. + double get third; + + /// The parameter `fourth` of this provider. + bool get fourth; + + /// The parameter `fifth` of this provider. + List? get fifth; +} + +class _FamilyProviderElement extends AutoDisposeStreamProviderElement + with FamilyRef { + _FamilyProviderElement(super.provider); + + @override + int get first => (origin as FamilyProvider).first; + @override + String? get second => (origin as FamilyProvider).second; + @override + double get third => (origin as FamilyProvider).third; + @override + bool get fourth => (origin as FamilyProvider).fourth; + @override + List? get fifth => (origin as FamilyProvider).fifth; +} + +String _$publicClassHash() => r'b1526943c8ff0aaa20642bf78e744e5833cf9d02'; /// See also [PublicClass]. @ProviderFor(PublicClass) @@ -200,7 +277,7 @@ final _privateClassProvider = ); typedef _$PrivateClass = AutoDisposeStreamNotifier; -String _$familyClassHash() => r'ece07693f90d6250513cdb3874ee9bfef35abd01'; +String _$familyClassHash() => r'6ec16ca23da8df4c010ecb5eed72e3e655504460'; abstract class _$FamilyClass extends BuildlessAutoDisposeStreamNotifier { @@ -278,12 +355,12 @@ class FamilyClassProvider extends AutoDisposeStreamNotifierProviderImpl { /// See also [FamilyClass]. FamilyClassProvider( - this.first, { - this.second, - required this.third, - this.fourth = true, - this.fifth, - }) : super.internal( + int first, { + String? second, + required double third, + bool fourth = true, + List? fifth, + }) : this._internal( () => FamilyClass() ..first = first ..second = second @@ -299,14 +376,77 @@ class FamilyClassProvider dependencies: FamilyClassFamily._dependencies, allTransitiveDependencies: FamilyClassFamily._allTransitiveDependencies, + first: first, + second: second, + third: third, + fourth: fourth, + fifth: fifth, ); + FamilyClassProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.first, + required this.second, + required this.third, + required this.fourth, + required this.fifth, + }) : super.internal(); + final int first; final String? second; final double third; final bool fourth; final List? fifth; + @override + Stream runNotifierBuild( + covariant FamilyClass notifier, + ) { + return notifier.build( + first, + second: second, + third: third, + fourth: fourth, + fifth: fifth, + ); + } + + @override + Override overrideWith(FamilyClass Function() create) { + return ProviderOverride( + origin: this, + override: FamilyClassProvider._internal( + () => create() + ..first = first + ..second = second + ..third = third + ..fourth = fourth + ..fifth = fifth, + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + first: first, + second: second, + third: third, + fourth: fourth, + fifth: fifth, + ), + ); + } + + @override + AutoDisposeStreamNotifierProviderElement + createElement() { + return _FamilyClassProviderElement(this); + } + @override bool operator ==(Object other) { return other is FamilyClassProvider && @@ -328,19 +468,40 @@ class FamilyClassProvider return _SystemHash.finish(hash); } +} + +mixin FamilyClassRef on AutoDisposeStreamNotifierProviderRef { + /// The parameter `first` of this provider. + int get first; + + /// The parameter `second` of this provider. + String? get second; + + /// The parameter `third` of this provider. + double get third; + + /// The parameter `fourth` of this provider. + bool get fourth; + + /// The parameter `fifth` of this provider. + List? get fifth; +} + +class _FamilyClassProviderElement + extends AutoDisposeStreamNotifierProviderElement + with FamilyClassRef { + _FamilyClassProviderElement(super.provider); @override - Stream runNotifierBuild( - covariant FamilyClass notifier, - ) { - return notifier.build( - first, - second: second, - third: third, - fourth: fourth, - fifth: fifth, - ); - } + int get first => (origin as FamilyClassProvider).first; + @override + String? get second => (origin as FamilyClassProvider).second; + @override + double get third => (origin as FamilyClassProvider).third; + @override + bool get fourth => (origin as FamilyClassProvider).fourth; + @override + List? get fifth => (origin as FamilyClassProvider).fifth; } // ignore_for_file: type=lint // ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member diff --git a/packages/riverpod_generator/test/integration/sync.dart b/packages/riverpod_generator/test/integration/sync.dart index 2cf69b39e..705aff627 100644 --- a/packages/riverpod_generator/test/integration/sync.dart +++ b/packages/riverpod_generator/test/integration/sync.dart @@ -88,6 +88,10 @@ String _private(_PrivateRef ref) { /// This is some documentation @riverpod class PublicClass extends _$PublicClass { + PublicClass([this.param]); + + final Object? param; + @override String build() { return 'Hello world'; @@ -107,6 +111,10 @@ class _PrivateClass extends _$PrivateClass { /// This is some documentation @riverpod class FamilyClass extends _$FamilyClass { + FamilyClass([this.param]); + + final Object? param; + @override String build( int first, { diff --git a/packages/riverpod_generator/test/integration/sync.g.dart b/packages/riverpod_generator/test/integration/sync.g.dart index d472f8c15..16aa17e1e 100644 --- a/packages/riverpod_generator/test/integration/sync.g.dart +++ b/packages/riverpod_generator/test/integration/sync.g.dart @@ -57,8 +57,6 @@ class _SystemHash { } } -typedef RawFamilyFutureRef = AutoDisposeProviderRef>; - /// See also [rawFamilyFuture]. @ProviderFor(rawFamilyFuture) const rawFamilyFutureProvider = RawFamilyFutureFamily(); @@ -105,10 +103,10 @@ class RawFamilyFutureFamily extends Family> { class RawFamilyFutureProvider extends AutoDisposeProvider> { /// See also [rawFamilyFuture]. RawFamilyFutureProvider( - this.id, - ) : super.internal( + int id, + ) : this._internal( (ref) => rawFamilyFuture( - ref, + ref as RawFamilyFutureRef, id, ), from: rawFamilyFutureProvider, @@ -120,10 +118,44 @@ class RawFamilyFutureProvider extends AutoDisposeProvider> { dependencies: RawFamilyFutureFamily._dependencies, allTransitiveDependencies: RawFamilyFutureFamily._allTransitiveDependencies, + id: id, ); + RawFamilyFutureProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.id, + }) : super.internal(); + final int id; + @override + Override overrideWith( + Future Function(RawFamilyFutureRef provider) create, + ) { + return ProviderOverride( + origin: this, + override: RawFamilyFutureProvider._internal( + (ref) => create(ref as RawFamilyFutureRef), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + id: id, + ), + ); + } + + @override + AutoDisposeProviderElement> createElement() { + return _RawFamilyFutureProviderElement(this); + } + @override bool operator ==(Object other) { return other is RawFamilyFutureProvider && other.id == id; @@ -138,8 +170,20 @@ class RawFamilyFutureProvider extends AutoDisposeProvider> { } } +mixin RawFamilyFutureRef on AutoDisposeProviderRef> { + /// The parameter `id` of this provider. + int get id; +} + +class _RawFamilyFutureProviderElement + extends AutoDisposeProviderElement> with RawFamilyFutureRef { + _RawFamilyFutureProviderElement(super.provider); + + @override + int get id => (origin as RawFamilyFutureProvider).id; +} + String _$rawFamilyStreamHash() => r'e778e5cfcb8ab381e2412f5c73213aaa03b93012'; -typedef RawFamilyStreamRef = AutoDisposeProviderRef>; /// See also [rawFamilyStream]. @ProviderFor(rawFamilyStream) @@ -187,10 +231,10 @@ class RawFamilyStreamFamily extends Family> { class RawFamilyStreamProvider extends AutoDisposeProvider> { /// See also [rawFamilyStream]. RawFamilyStreamProvider( - this.id, - ) : super.internal( + int id, + ) : this._internal( (ref) => rawFamilyStream( - ref, + ref as RawFamilyStreamRef, id, ), from: rawFamilyStreamProvider, @@ -202,10 +246,44 @@ class RawFamilyStreamProvider extends AutoDisposeProvider> { dependencies: RawFamilyStreamFamily._dependencies, allTransitiveDependencies: RawFamilyStreamFamily._allTransitiveDependencies, + id: id, ); + RawFamilyStreamProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.id, + }) : super.internal(); + final int id; + @override + Override overrideWith( + Stream Function(RawFamilyStreamRef provider) create, + ) { + return ProviderOverride( + origin: this, + override: RawFamilyStreamProvider._internal( + (ref) => create(ref as RawFamilyStreamRef), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + id: id, + ), + ); + } + + @override + AutoDisposeProviderElement> createElement() { + return _RawFamilyStreamProviderElement(this); + } + @override bool operator ==(Object other) { return other is RawFamilyStreamProvider && other.id == id; @@ -220,6 +298,19 @@ class RawFamilyStreamProvider extends AutoDisposeProvider> { } } +mixin RawFamilyStreamRef on AutoDisposeProviderRef> { + /// The parameter `id` of this provider. + int get id; +} + +class _RawFamilyStreamProviderElement + extends AutoDisposeProviderElement> with RawFamilyStreamRef { + _RawFamilyStreamProviderElement(super.provider); + + @override + int get id => (origin as RawFamilyStreamProvider).id; +} + String _$publicHash() => r'138be35943899793ab085e711fe3f3d22696a3ba'; /// This is some documentation @@ -252,7 +343,6 @@ final supports$inNamesProvider = AutoDisposeProvider.internal( typedef Supports$inNamesRef = AutoDisposeProviderRef; String _$familyHash() => r'14d1ee238ca608d547630d0e222ef4c5866e9e61'; -typedef FamilyRef = AutoDisposeProviderRef; /// This is some documentation /// @@ -324,14 +414,14 @@ class FamilyProvider extends AutoDisposeProvider { /// /// Copied from [family]. FamilyProvider( - this.first, { - this.second, - required this.third, - this.fourth = true, - this.fifth, - }) : super.internal( + int first, { + String? second, + required double third, + bool fourth = true, + List? fifth, + }) : this._internal( (ref) => family( - ref, + ref as FamilyRef, first, second: second, third: third, @@ -346,14 +436,60 @@ class FamilyProvider extends AutoDisposeProvider { : _$familyHash, dependencies: FamilyFamily._dependencies, allTransitiveDependencies: FamilyFamily._allTransitiveDependencies, + first: first, + second: second, + third: third, + fourth: fourth, + fifth: fifth, ); + FamilyProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.first, + required this.second, + required this.third, + required this.fourth, + required this.fifth, + }) : super.internal(); + final int first; final String? second; final double third; final bool fourth; final List? fifth; + @override + Override overrideWith( + String Function(FamilyRef provider) create, + ) { + return ProviderOverride( + origin: this, + override: FamilyProvider._internal( + (ref) => create(ref as FamilyRef), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + first: first, + second: second, + third: third, + fourth: fourth, + fifth: fifth, + ), + ); + } + + @override + AutoDisposeProviderElement createElement() { + return _FamilyProviderElement(this); + } + @override bool operator ==(Object other) { return other is FamilyProvider && @@ -377,6 +513,39 @@ class FamilyProvider extends AutoDisposeProvider { } } +mixin FamilyRef on AutoDisposeProviderRef { + /// The parameter `first` of this provider. + int get first; + + /// The parameter `second` of this provider. + String? get second; + + /// The parameter `third` of this provider. + double get third; + + /// The parameter `fourth` of this provider. + bool get fourth; + + /// The parameter `fifth` of this provider. + List? get fifth; +} + +class _FamilyProviderElement extends AutoDisposeProviderElement + with FamilyRef { + _FamilyProviderElement(super.provider); + + @override + int get first => (origin as FamilyProvider).first; + @override + String? get second => (origin as FamilyProvider).second; + @override + double get third => (origin as FamilyProvider).third; + @override + bool get fourth => (origin as FamilyProvider).fourth; + @override + List? get fifth => (origin as FamilyProvider).fifth; +} + String _$privateHash() => r'519561bc7e88e394d7f75ca2102a5c0acc832c66'; /// See also [_private]. @@ -496,8 +665,8 @@ class RawFamilyFutureClassProvider extends AutoDisposeNotifierProviderImpl< RawFamilyFutureClass, Future> { /// See also [RawFamilyFutureClass]. RawFamilyFutureClassProvider( - this.id, - ) : super.internal( + int id, + ) : this._internal( () => RawFamilyFutureClass()..id = id, from: rawFamilyFutureClassProvider, name: r'rawFamilyFutureClassProvider', @@ -508,10 +677,52 @@ class RawFamilyFutureClassProvider extends AutoDisposeNotifierProviderImpl< dependencies: RawFamilyFutureClassFamily._dependencies, allTransitiveDependencies: RawFamilyFutureClassFamily._allTransitiveDependencies, + id: id, ); + RawFamilyFutureClassProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.id, + }) : super.internal(); + final int id; + @override + Future runNotifierBuild( + covariant RawFamilyFutureClass notifier, + ) { + return notifier.build( + id, + ); + } + + @override + Override overrideWith(RawFamilyFutureClass Function() create) { + return ProviderOverride( + origin: this, + override: RawFamilyFutureClassProvider._internal( + () => create()..id = id, + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + id: id, + ), + ); + } + + @override + AutoDisposeNotifierProviderElement> + createElement() { + return _RawFamilyFutureClassProviderElement(this); + } + @override bool operator ==(Object other) { return other is RawFamilyFutureClassProvider && other.id == id; @@ -524,15 +735,21 @@ class RawFamilyFutureClassProvider extends AutoDisposeNotifierProviderImpl< return _SystemHash.finish(hash); } +} + +mixin RawFamilyFutureClassRef + on AutoDisposeNotifierProviderRef> { + /// The parameter `id` of this provider. + int get id; +} + +class _RawFamilyFutureClassProviderElement + extends AutoDisposeNotifierProviderElement> with RawFamilyFutureClassRef { + _RawFamilyFutureClassProviderElement(super.provider); @override - Future runNotifierBuild( - covariant RawFamilyFutureClass notifier, - ) { - return notifier.build( - id, - ); - } + int get id => (origin as RawFamilyFutureClassProvider).id; } String _$rawFamilyStreamClassHash() => @@ -594,8 +811,8 @@ class RawFamilyStreamClassProvider extends AutoDisposeNotifierProviderImpl< RawFamilyStreamClass, Stream> { /// See also [RawFamilyStreamClass]. RawFamilyStreamClassProvider( - this.id, - ) : super.internal( + int id, + ) : this._internal( () => RawFamilyStreamClass()..id = id, from: rawFamilyStreamClassProvider, name: r'rawFamilyStreamClassProvider', @@ -606,10 +823,52 @@ class RawFamilyStreamClassProvider extends AutoDisposeNotifierProviderImpl< dependencies: RawFamilyStreamClassFamily._dependencies, allTransitiveDependencies: RawFamilyStreamClassFamily._allTransitiveDependencies, + id: id, ); + RawFamilyStreamClassProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.id, + }) : super.internal(); + final int id; + @override + Stream runNotifierBuild( + covariant RawFamilyStreamClass notifier, + ) { + return notifier.build( + id, + ); + } + + @override + Override overrideWith(RawFamilyStreamClass Function() create) { + return ProviderOverride( + origin: this, + override: RawFamilyStreamClassProvider._internal( + () => create()..id = id, + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + id: id, + ), + ); + } + + @override + AutoDisposeNotifierProviderElement> + createElement() { + return _RawFamilyStreamClassProviderElement(this); + } + @override bool operator ==(Object other) { return other is RawFamilyStreamClassProvider && other.id == id; @@ -622,18 +881,24 @@ class RawFamilyStreamClassProvider extends AutoDisposeNotifierProviderImpl< return _SystemHash.finish(hash); } +} + +mixin RawFamilyStreamClassRef + on AutoDisposeNotifierProviderRef> { + /// The parameter `id` of this provider. + int get id; +} + +class _RawFamilyStreamClassProviderElement + extends AutoDisposeNotifierProviderElement> with RawFamilyStreamClassRef { + _RawFamilyStreamClassProviderElement(super.provider); @override - Stream runNotifierBuild( - covariant RawFamilyStreamClass notifier, - ) { - return notifier.build( - id, - ); - } + int get id => (origin as RawFamilyStreamClassProvider).id; } -String _$publicClassHash() => r'f04884c039e6200ad3537feeecfc6e83828b5eb5'; +String _$publicClassHash() => r'c8e7eec9e202acf8394e02496857cbe49405bf62'; /// This is some documentation /// @@ -665,7 +930,7 @@ final _privateClassProvider = ); typedef _$PrivateClass = AutoDisposeNotifier; -String _$familyClassHash() => r'7dd0013dba8f45e82e8e39fbb2635e5a7f4b9cac'; +String _$familyClassHash() => r'01e3b9cb4d6d0bf12a2284761b1a11819d97d249'; abstract class _$FamilyClass extends BuildlessAutoDisposeNotifier { late final int first; @@ -754,12 +1019,12 @@ class FamilyClassProvider /// /// Copied from [FamilyClass]. FamilyClassProvider( - this.first, { - this.second, - required this.third, - this.fourth = true, - this.fifth, - }) : super.internal( + int first, { + String? second, + required double third, + bool fourth = true, + List? fifth, + }) : this._internal( () => FamilyClass() ..first = first ..second = second @@ -775,14 +1040,76 @@ class FamilyClassProvider dependencies: FamilyClassFamily._dependencies, allTransitiveDependencies: FamilyClassFamily._allTransitiveDependencies, + first: first, + second: second, + third: third, + fourth: fourth, + fifth: fifth, ); + FamilyClassProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.first, + required this.second, + required this.third, + required this.fourth, + required this.fifth, + }) : super.internal(); + final int first; final String? second; final double third; final bool fourth; final List? fifth; + @override + String runNotifierBuild( + covariant FamilyClass notifier, + ) { + return notifier.build( + first, + second: second, + third: third, + fourth: fourth, + fifth: fifth, + ); + } + + @override + Override overrideWith(FamilyClass Function() create) { + return ProviderOverride( + origin: this, + override: FamilyClassProvider._internal( + () => create() + ..first = first + ..second = second + ..third = third + ..fourth = fourth + ..fifth = fifth, + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + first: first, + second: second, + third: third, + fourth: fourth, + fifth: fifth, + ), + ); + } + + @override + AutoDisposeNotifierProviderElement createElement() { + return _FamilyClassProviderElement(this); + } + @override bool operator ==(Object other) { return other is FamilyClassProvider && @@ -804,19 +1131,40 @@ class FamilyClassProvider return _SystemHash.finish(hash); } +} + +mixin FamilyClassRef on AutoDisposeNotifierProviderRef { + /// The parameter `first` of this provider. + int get first; + + /// The parameter `second` of this provider. + String? get second; + + /// The parameter `third` of this provider. + double get third; + + /// The parameter `fourth` of this provider. + bool get fourth; + + /// The parameter `fifth` of this provider. + List? get fifth; +} + +class _FamilyClassProviderElement + extends AutoDisposeNotifierProviderElement + with FamilyClassRef { + _FamilyClassProviderElement(super.provider); @override - String runNotifierBuild( - covariant FamilyClass notifier, - ) { - return notifier.build( - first, - second: second, - third: third, - fourth: fourth, - fifth: fifth, - ); - } + int get first => (origin as FamilyClassProvider).first; + @override + String? get second => (origin as FamilyClassProvider).second; + @override + double get third => (origin as FamilyClassProvider).third; + @override + bool get fourth => (origin as FamilyClassProvider).fourth; + @override + List? get fifth => (origin as FamilyClassProvider).fifth; } String _$supports$InClassNameHash() => diff --git a/packages/riverpod_generator/test/notifier_test.dart b/packages/riverpod_generator/test/notifier_test.dart index a84d5eea2..bdeaf0fd1 100644 --- a/packages/riverpod_generator/test/notifier_test.dart +++ b/packages/riverpod_generator/test/notifier_test.dart @@ -26,6 +26,41 @@ void main() { expect(familyClassProvider(42, third: .42).name, 'familyClassProvider'); }); + test('Supports overriding non-family notifiers', () { + final container = createContainer( + overrides: [ + publicClassProvider.overrideWith(() => PublicClass('Hello world')), + ], + ); + + final notifier = container.read(publicClassProvider.notifier); + expect(notifier.param, 'Hello world'); + + expect(notifier.ref, isNotNull); + expect(notifier.state, isNotNull); + }); + + test('Supports overriding family notifiers', () { + final container = createContainer( + overrides: [ + familyClassProvider(42, third: .42) + .overrideWith(() => FamilyClass('Hello world')), + ], + ); + + final notifier = + container.read(familyClassProvider(42, third: .42).notifier); + expect(notifier.param, 'Hello world'); + expect(notifier.first, 42); + expect(notifier.second, null); + expect(notifier.third, .42); + expect(notifier.fourth, true); + expect(notifier.fifth, null); + + expect(notifier.ref, isNotNull); + expect(notifier.state, isNotNull); + }); + test( 'Creates a NotifierProvider.family if @riverpod is used on a synchronous function with parameters', () { diff --git a/packages/riverpod_generator/test/stream_notifier_test.dart b/packages/riverpod_generator/test/stream_notifier_test.dart index 9e4bb583e..d066c4a8d 100644 --- a/packages/riverpod_generator/test/stream_notifier_test.dart +++ b/packages/riverpod_generator/test/stream_notifier_test.dart @@ -29,6 +29,41 @@ void main() { expect(familyClassProvider(42, third: .42).name, 'familyClassProvider'); }); + test('Supports overriding non-family notifiers', () { + final container = createContainer( + overrides: [ + publicClassProvider.overrideWith(() => PublicClass('Hello world')), + ], + ); + + final notifier = container.read(publicClassProvider.notifier); + expect(notifier.param, 'Hello world'); + + expect(notifier.ref, isNotNull); + expect(notifier.state, isNotNull); + }); + + test('Supports overriding family notifiers', () { + final container = createContainer( + overrides: [ + familyClassProvider(42, third: .42) + .overrideWith(() => FamilyClass('Hello world')), + ], + ); + + final notifier = + container.read(familyClassProvider(42, third: .42).notifier); + expect(notifier.param, 'Hello world'); + expect(notifier.first, 42); + expect(notifier.second, null); + expect(notifier.third, .42); + expect(notifier.fourth, true); + expect(notifier.fifth, null); + + expect(notifier.ref, isNotNull); + expect(notifier.state, isNotNull); + }); + test( 'Creates a NotifierProvider.family if @riverpod is used on a synchronous function with parameters', () async { diff --git a/packages/riverpod_generator/test/stream_test.dart b/packages/riverpod_generator/test/stream_test.dart index 97998b2b7..fa6d54ee2 100644 --- a/packages/riverpod_generator/test/stream_test.dart +++ b/packages/riverpod_generator/test/stream_test.dart @@ -27,6 +27,33 @@ void main() { expect(familyProvider(42, third: .42).name, 'familyProvider'); }); + test('Supports overriding non-family providers', () async { + final container = createContainer( + overrides: [ + publicProvider.overrideWith((ref) => Stream.value('Hello world')), + ], + ); + + final result = container.read(publicProvider.future); + expect(await result, 'Hello world'); + }); + + test('Supports overriding family providers', () async { + final container = createContainer( + overrides: [ + familyProvider(42, third: .42).overrideWith( + (ref) => Stream.value( + 'Hello world ${ref.first} ${ref.second} ' + '${ref.third} ${ref.fourth} ${ref.fifth}', + ), + ), + ], + ); + + final result = container.read(familyProvider(42, third: .42).future); + expect(await result, 'Hello world 42 null 0.42 true null'); + }); + test( 'Creates a Provider.family if @riverpod is used on a synchronous function with parameters', () async { diff --git a/packages/riverpod_generator/test/sync_test.dart b/packages/riverpod_generator/test/sync_test.dart index 2e1f5399e..af406d70b 100644 --- a/packages/riverpod_generator/test/sync_test.dart +++ b/packages/riverpod_generator/test/sync_test.dart @@ -65,6 +65,31 @@ void main() { ); }); + test('Supports overriding non-family providers', () { + final container = createContainer( + overrides: [ + publicProvider.overrideWith((ref) => 'Hello world'), + ], + ); + + final result = container.read(publicProvider); + expect(result, 'Hello world'); + }); + + test('Supports overriding family providers', () { + final container = createContainer( + overrides: [ + familyProvider(42, third: .42).overrideWith( + (ref) => 'Hello world ${ref.first} ${ref.second} ' + '${ref.third} ${ref.fourth} ${ref.fifth}', + ), + ], + ); + + final result = container.read(familyProvider(42, third: .42)); + expect(result, 'Hello world 42 null 0.42 true null'); + }); + test( 'Creates a Provider if @riverpod is used on an stream function wrapped in Raw', () async { diff --git a/packages/riverpod_graph/test/integration/generated/golden/lib/sync.g.dart b/packages/riverpod_graph/test/integration/generated/golden/lib/sync.g.dart index 454dc7e8f..c23c840f8 100644 --- a/packages/riverpod_graph/test/integration/generated/golden/lib/sync.g.dart +++ b/packages/riverpod_graph/test/integration/generated/golden/lib/sync.g.dart @@ -62,8 +62,6 @@ class _SystemHash { } } -typedef FamilyRef = AutoDisposeProviderRef; - /// A generated family provider. /// /// Copied from [family]. @@ -134,14 +132,14 @@ class FamilyProvider extends AutoDisposeProvider { /// /// Copied from [family]. FamilyProvider( - this.first, { - this.second, - required this.third, - this.forth = true, - this.fifth, - }) : super.internal( + int first, { + String? second, + required double third, + bool forth = true, + List? fifth, + }) : this._internal( (ref) => family( - ref, + ref as FamilyRef, first, second: second, third: third, @@ -156,14 +154,60 @@ class FamilyProvider extends AutoDisposeProvider { : _$familyHash, dependencies: FamilyFamily._dependencies, allTransitiveDependencies: FamilyFamily._allTransitiveDependencies, + first: first, + second: second, + third: third, + forth: forth, + fifth: fifth, ); + FamilyProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.first, + required this.second, + required this.third, + required this.forth, + required this.fifth, + }) : super.internal(); + final int first; final String? second; final double third; final bool forth; final List? fifth; + @override + Override overrideWith( + String Function(FamilyRef provider) create, + ) { + return ProviderOverride( + origin: this, + override: FamilyProvider._internal( + (ref) => create(ref as FamilyRef), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + first: first, + second: second, + third: third, + forth: forth, + fifth: fifth, + ), + ); + } + + @override + AutoDisposeProviderElement createElement() { + return _FamilyProviderElement(this); + } + @override bool operator ==(Object other) { return other is FamilyProvider && @@ -187,6 +231,39 @@ class FamilyProvider extends AutoDisposeProvider { } } +mixin FamilyRef on AutoDisposeProviderRef { + /// The parameter `first` of this provider. + int get first; + + /// The parameter `second` of this provider. + String? get second; + + /// The parameter `third` of this provider. + double get third; + + /// The parameter `forth` of this provider. + bool get forth; + + /// The parameter `fifth` of this provider. + List? get fifth; +} + +class _FamilyProviderElement extends AutoDisposeProviderElement + with FamilyRef { + _FamilyProviderElement(super.provider); + + @override + int get first => (origin as FamilyProvider).first; + @override + String? get second => (origin as FamilyProvider).second; + @override + double get third => (origin as FamilyProvider).third; + @override + bool get forth => (origin as FamilyProvider).forth; + @override + List? get fifth => (origin as FamilyProvider).fifth; +} + String _$privateHash() => r'9a87ed0765ad8448525fa1290b34760c79e7402b'; /// See also [_private]. @@ -322,12 +399,12 @@ class FamilyClassProvider /// /// Copied from [FamilyClass]. FamilyClassProvider( - this.first, { - this.second, - required this.third, - this.forth = true, - this.fifth, - }) : super.internal( + int first, { + String? second, + required double third, + bool forth = true, + List? fifth, + }) : this._internal( () => FamilyClass() ..first = first ..second = second @@ -343,14 +420,76 @@ class FamilyClassProvider dependencies: FamilyClassFamily._dependencies, allTransitiveDependencies: FamilyClassFamily._allTransitiveDependencies, + first: first, + second: second, + third: third, + forth: forth, + fifth: fifth, ); + FamilyClassProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.first, + required this.second, + required this.third, + required this.forth, + required this.fifth, + }) : super.internal(); + final int first; final String? second; final double third; final bool forth; final List? fifth; + @override + String runNotifierBuild( + covariant FamilyClass notifier, + ) { + return notifier.build( + first, + second: second, + third: third, + forth: forth, + fifth: fifth, + ); + } + + @override + Override overrideWith(FamilyClass Function() create) { + return ProviderOverride( + origin: this, + override: FamilyClassProvider._internal( + () => create() + ..first = first + ..second = second + ..third = third + ..forth = forth + ..fifth = fifth, + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + first: first, + second: second, + third: third, + forth: forth, + fifth: fifth, + ), + ); + } + + @override + AutoDisposeNotifierProviderElement createElement() { + return _FamilyClassProviderElement(this); + } + @override bool operator ==(Object other) { return other is FamilyClassProvider && @@ -372,19 +511,40 @@ class FamilyClassProvider return _SystemHash.finish(hash); } +} + +mixin FamilyClassRef on AutoDisposeNotifierProviderRef { + /// The parameter `first` of this provider. + int get first; + + /// The parameter `second` of this provider. + String? get second; + + /// The parameter `third` of this provider. + double get third; + + /// The parameter `forth` of this provider. + bool get forth; + + /// The parameter `fifth` of this provider. + List? get fifth; +} + +class _FamilyClassProviderElement + extends AutoDisposeNotifierProviderElement + with FamilyClassRef { + _FamilyClassProviderElement(super.provider); @override - String runNotifierBuild( - covariant FamilyClass notifier, - ) { - return notifier.build( - first, - second: second, - third: third, - forth: forth, - fifth: fifth, - ); - } + int get first => (origin as FamilyClassProvider).first; + @override + String? get second => (origin as FamilyClassProvider).second; + @override + double get third => (origin as FamilyClassProvider).third; + @override + bool get forth => (origin as FamilyClassProvider).forth; + @override + List? get fifth => (origin as FamilyClassProvider).fifth; } String _$supports$InClassNameHash() => diff --git a/packages/riverpod_lint_flutter_test/test/assists/convert_class_based_provider_to_functional.g.dart b/packages/riverpod_lint_flutter_test/test/assists/convert_class_based_provider_to_functional.g.dart index 0b6486217..117a27a68 100644 --- a/packages/riverpod_lint_flutter_test/test/assists/convert_class_based_provider_to_functional.g.dart +++ b/packages/riverpod_lint_flutter_test/test/assists/convert_class_based_provider_to_functional.g.dart @@ -117,9 +117,9 @@ class ExampleFamilyProvider /// /// Copied from [ExampleFamily]. ExampleFamilyProvider({ - required this.a, - this.b = '42', - }) : super.internal( + required int a, + String b = '42', + }) : this._internal( () => ExampleFamily() ..a = a ..b = b, @@ -132,11 +132,58 @@ class ExampleFamilyProvider dependencies: ExampleFamilyFamily._dependencies, allTransitiveDependencies: ExampleFamilyFamily._allTransitiveDependencies, + a: a, + b: b, ); + ExampleFamilyProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.a, + required this.b, + }) : super.internal(); + final int a; final String b; + @override + int runNotifierBuild( + covariant ExampleFamily notifier, + ) { + return notifier.build( + a: a, + b: b, + ); + } + + @override + Override overrideWith(ExampleFamily Function() create) { + return ProviderOverride( + origin: this, + override: ExampleFamilyProvider._internal( + () => create() + ..a = a + ..b = b, + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + a: a, + b: b, + ), + ); + } + + @override + AutoDisposeNotifierProviderElement createElement() { + return _ExampleFamilyProviderElement(this); + } + @override bool operator ==(Object other) { return other is ExampleFamilyProvider && other.a == a && other.b == b; @@ -150,16 +197,25 @@ class ExampleFamilyProvider return _SystemHash.finish(hash); } +} + +mixin ExampleFamilyRef on AutoDisposeNotifierProviderRef { + /// The parameter `a` of this provider. + int get a; + + /// The parameter `b` of this provider. + String get b; +} + +class _ExampleFamilyProviderElement + extends AutoDisposeNotifierProviderElement + with ExampleFamilyRef { + _ExampleFamilyProviderElement(super.provider); @override - int runNotifierBuild( - covariant ExampleFamily notifier, - ) { - return notifier.build( - a: a, - b: b, - ); - } + int get a => (origin as ExampleFamilyProvider).a; + @override + String get b => (origin as ExampleFamilyProvider).b; } // ignore_for_file: type=lint // ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member diff --git a/packages/riverpod_lint_flutter_test/test/assists/convert_functional_provider_to_class_based.g.dart b/packages/riverpod_lint_flutter_test/test/assists/convert_functional_provider_to_class_based.g.dart index 46fca5bd8..e7496c479 100644 --- a/packages/riverpod_lint_flutter_test/test/assists/convert_functional_provider_to_class_based.g.dart +++ b/packages/riverpod_lint_flutter_test/test/assists/convert_functional_provider_to_class_based.g.dart @@ -45,8 +45,6 @@ class _SystemHash { } } -typedef ExampleFamilyRef = AutoDisposeProviderRef; - /// Some comment /// /// Copied from [exampleFamily]. @@ -108,11 +106,11 @@ class ExampleFamilyProvider extends AutoDisposeProvider { /// /// Copied from [exampleFamily]. ExampleFamilyProvider({ - required this.a, - this.b = '42', - }) : super.internal( + required int a, + String b = '42', + }) : this._internal( (ref) => exampleFamily( - ref, + ref as ExampleFamilyRef, a: a, b: b, ), @@ -125,11 +123,48 @@ class ExampleFamilyProvider extends AutoDisposeProvider { dependencies: ExampleFamilyFamily._dependencies, allTransitiveDependencies: ExampleFamilyFamily._allTransitiveDependencies, + a: a, + b: b, ); + ExampleFamilyProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.a, + required this.b, + }) : super.internal(); + final int a; final String b; + @override + Override overrideWith( + int Function(ExampleFamilyRef provider) create, + ) { + return ProviderOverride( + origin: this, + override: ExampleFamilyProvider._internal( + (ref) => create(ref as ExampleFamilyRef), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + a: a, + b: b, + ), + ); + } + + @override + AutoDisposeProviderElement createElement() { + return _ExampleFamilyProviderElement(this); + } + @override bool operator ==(Object other) { return other is ExampleFamilyProvider && other.a == a && other.b == b; @@ -144,5 +179,23 @@ class ExampleFamilyProvider extends AutoDisposeProvider { return _SystemHash.finish(hash); } } + +mixin ExampleFamilyRef on AutoDisposeProviderRef { + /// The parameter `a` of this provider. + int get a; + + /// The parameter `b` of this provider. + String get b; +} + +class _ExampleFamilyProviderElement extends AutoDisposeProviderElement + with ExampleFamilyRef { + _ExampleFamilyProviderElement(super.provider); + + @override + int get a => (origin as ExampleFamilyProvider).a; + @override + String get b => (origin as ExampleFamilyProvider).b; +} // ignore_for_file: type=lint // ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member diff --git a/packages/riverpod_lint_flutter_test/test/goldens/lints/provider_parameters.g.dart b/packages/riverpod_lint_flutter_test/test/goldens/lints/provider_parameters.g.dart index bc914c368..8fa54ecc9 100644 --- a/packages/riverpod_lint_flutter_test/test/goldens/lints/provider_parameters.g.dart +++ b/packages/riverpod_lint_flutter_test/test/goldens/lints/provider_parameters.g.dart @@ -29,8 +29,6 @@ class _SystemHash { } } -typedef GeneratorRef = ProviderRef; - /// See also [generator]. @ProviderFor(generator) const generatorProvider = GeneratorFamily(); @@ -77,10 +75,10 @@ class GeneratorFamily extends Family { class GeneratorProvider extends Provider { /// See also [generator]. GeneratorProvider({ - this.value, - }) : super.internal( + Object? value, + }) : this._internal( (ref) => generator( - ref, + ref as GeneratorRef, value: value, ), from: generatorProvider, @@ -91,10 +89,44 @@ class GeneratorProvider extends Provider { : _$generatorHash, dependencies: GeneratorFamily._dependencies, allTransitiveDependencies: GeneratorFamily._allTransitiveDependencies, + value: value, ); + GeneratorProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.value, + }) : super.internal(); + final Object? value; + @override + Override overrideWith( + int Function(GeneratorRef provider) create, + ) { + return ProviderOverride( + origin: this, + override: GeneratorProvider._internal( + (ref) => create(ref as GeneratorRef), + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + value: value, + ), + ); + } + + @override + ProviderElement createElement() { + return _GeneratorProviderElement(this); + } + @override bool operator ==(Object other) { return other is GeneratorProvider && other.value == value; @@ -108,5 +140,17 @@ class GeneratorProvider extends Provider { return _SystemHash.finish(hash); } } + +mixin GeneratorRef on ProviderRef { + /// The parameter `value` of this provider. + Object? get value; +} + +class _GeneratorProviderElement extends ProviderElement with GeneratorRef { + _GeneratorProviderElement(super.provider); + + @override + Object? get value => (origin as GeneratorProvider).value; +} // ignore_for_file: type=lint // ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member