Skip to content

Commit

Permalink
Merge pull request #3412 from rrousselGit/remove-parent
Browse files Browse the repository at this point in the history
Remove deprecated members
  • Loading branch information
rrousselGit authored Mar 10, 2024
2 parents 0aefb65 + edf2bd8 commit e72869e
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 211 deletions.
1 change: 1 addition & 0 deletions packages/flutter_riverpod/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased build

- Added support for `Ref/ProviderContainer.invalidate(provider, asReload: true)`
- Removed deprecated `@ProviderScope.parent`

## 3.0.0-dev.3 - 2023-11-27

Expand Down
64 changes: 5 additions & 59 deletions packages/flutter_riverpod/lib/src/core/provider_scope.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: invalid_use_of_internal_member, deprecated_member_use_from_same_package
// ignore_for_file: invalid_use_of_internal_member
part of '../core.dart';

/// {@template riverpod.provider_scope}
Expand Down Expand Up @@ -75,10 +75,6 @@ class ProviderScope extends StatefulWidget {
super.key,
this.overrides = const [],
this.observers,
@Deprecated(
'Will be removed in 3.0.0. See https://github.com/rrousselGit/riverpod/issues/3261#issuecomment-1973514033',
)
this.parent,
required this.child,
});

Expand All @@ -105,37 +101,6 @@ class ProviderScope extends StatefulWidget {
return scope.container;
}

/// Explicitly override the parent [ProviderContainer] that this [ProviderScope]
/// would be a descendant of.
///
/// A common use-case is to allow modals to access scoped providers, as they
/// would otherwise be unable to since they would be in a different branch
/// of the widget tree.
///
/// That can be achieved with:
///
/// ```dart
/// ElevatedButton(
/// onTap: () {
/// final container = ProviderScope.containerOf(context);
/// showDialog(
/// context: context,
/// builder: (context) {
/// return ProviderScope(parent: container, child: MyModal());
/// },
/// );
/// },
/// child: Text('show modal'),
/// )
/// ```
///
///
/// The [parent] variable must never change.
@Deprecated(
'Will be removed in 3.0.0. See https://github.com/rrousselGit/riverpod/issues/3261#issuecomment-1973514033',
)
final ProviderContainer? parent;

/// The part of the widget tree that can use Riverpod and has overridden providers.
final Widget child;

Expand Down Expand Up @@ -198,39 +163,20 @@ final class ProviderScopeState extends State<ProviderScope> {
}

ProviderContainer? _getParent() {
if (widget.parent != null) {
return widget.parent;
} else {
final scope = context
.getElementForInheritedWidgetOfExactType<UncontrolledProviderScope>()
?.widget as UncontrolledProviderScope?;
final scope = context
.getElementForInheritedWidgetOfExactType<UncontrolledProviderScope>()
?.widget as UncontrolledProviderScope?;

return scope?.container;
}
return scope?.container;
}

@override
void didUpdateWidget(ProviderScope oldWidget) {
super.didUpdateWidget(oldWidget);
_dirty = true;

if (oldWidget.parent != widget.parent) {
FlutterError.reportError(
FlutterErrorDetails(
library: 'flutter_riverpod',
exception: UnsupportedError(
'Changing ProviderScope.parent is not supported',
),
context: ErrorDescription('while rebuilding ProviderScope'),
),
);
}
}

void _debugAssertParentDidNotChange() {
// didUpdateWidget already takes care of widget.parent change
if (widget.parent != null) return;

final parent = _getParent();

if (parent != _debugParentOwner) {
Expand Down
48 changes: 0 additions & 48 deletions packages/flutter_riverpod/test/framework_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,54 +154,6 @@ void main() {
verifyOnly(listener, listener(0, 1));
});

testWidgets('ProviderScope can receive a custom parent', (tester) async {
final provider = Provider((ref) => 0);

final container = createContainer(
overrides: [provider.overrideWithValue(42)],
);

await tester.pumpWidget(
ProviderScope(
// ignore: deprecated_member_use_from_same_package
parent: container,
child: Consumer(
builder: (context, ref, _) {
return Text(
'${ref.watch(provider)}',
textDirection: TextDirection.ltr,
);
},
),
),
);

expect(find.text('42'), findsOneWidget);
});

testWidgets('ProviderScope.parent cannot change', (tester) async {
final container = createContainer();
final container2 = createContainer();

await tester.pumpWidget(
ProviderScope(
// ignore: deprecated_member_use_from_same_package
parent: container,
child: Container(),
),
);

await tester.pumpWidget(
ProviderScope(
// ignore: deprecated_member_use_from_same_package
parent: container2,
child: Container(),
),
);

expect(tester.takeException(), isUnsupportedError);
});

testWidgets('ref.read works with providers that returns null',
(tester) async {
final nullProvider = Provider((ref) => null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,6 @@ class ScopedProvidersShouldSpecifyDependencies extends RiverpodLintRule {
bool isProviderScopeScoped(
ProviderScopeInstanceCreationExpression expression,
) {
final hasParentParameter = expression.node.argumentList.arguments
.whereType<NamedExpression>()
// TODO move to riverpod_analyzer_utils
// TODO handle parent:null.
// This might be doable by checking that the expression's
// static type is non-nullable
.any((e) => e.name.label.name == 'parent');
if (hasParentParameter) return true;

// in runApp(ProviderScope(..)) the direct parent of the ProviderScope
// is an ArgumentList.
final enclosingExpression = expression.node.parent?.parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,7 @@ test/lints/scoped_providers_should_specify_dependencies.dart:67:9
code: scoped_providers_should_specify_dependencies
severity: Severity.warning
message: Providers which are overridden in a non-root ProviderContainer/ProviderScope should specify dependencies.
test/lints/scoped_providers_should_specify_dependencies.dart:82:9

```dart
.overrideWith(() => throw UnimplementedError()),
// expect_lint: scoped_providers_should_specify_dependencies
>>>rootProvider.overrideWith((ref) => 0)<<<,
],
child: Container(),
```

=======

code: scoped_providers_should_specify_dependencies
severity: Severity.warning
message: Providers which are overridden in a non-root ProviderContainer/ProviderScope should specify dependencies.
test/lints/scoped_providers_should_specify_dependencies.dart:107:7
test/lints/scoped_providers_should_specify_dependencies.dart:92:7

```dart
.overrideWith(() => throw UnimplementedError()),
Expand All @@ -61,37 +46,7 @@ test/lints/scoped_providers_should_specify_dependencies.dart:107:7
code: scoped_providers_should_specify_dependencies
severity: Severity.warning
message: Providers which are overridden in a non-root ProviderContainer/ProviderScope should specify dependencies.
test/lints/scoped_providers_should_specify_dependencies.dart:132:9

```dart
.overrideWith(() => throw UnimplementedError()),
// expect_lint: scoped_providers_should_specify_dependencies
>>>rootProvider.overrideWith((ref) => 0)<<<,
],
child: Container(),
```

=======

code: scoped_providers_should_specify_dependencies
severity: Severity.warning
message: Providers which are overridden in a non-root ProviderContainer/ProviderScope should specify dependencies.
test/lints/scoped_providers_should_specify_dependencies.dart:171:11

```dart
.overrideWith(() => throw UnimplementedError()),
// expect_lint: scoped_providers_should_specify_dependencies
>>>rootProvider.overrideWith((ref) => 0)<<<,
],
child: Container(),
```

=======

code: scoped_providers_should_specify_dependencies
severity: Severity.warning
message: Providers which are overridden in a non-root ProviderContainer/ProviderScope should specify dependencies.
test/lints/scoped_providers_should_specify_dependencies.dart:185:13
test/lints/scoped_providers_should_specify_dependencies.dart:140:13

```dart
.overrideWith(() => throw UnimplementedError()),
Expand All @@ -106,7 +61,7 @@ test/lints/scoped_providers_should_specify_dependencies.dart:185:13
code: scoped_providers_should_specify_dependencies
severity: Severity.warning
message: Providers which are overridden in a non-root ProviderContainer/ProviderScope should specify dependencies.
test/lints/scoped_providers_should_specify_dependencies.dart:201:7
test/lints/scoped_providers_should_specify_dependencies.dart:156:7

```dart
.overrideWith(() => throw UnimplementedError()),
Expand All @@ -121,7 +76,7 @@ test/lints/scoped_providers_should_specify_dependencies.dart:201:7
code: scoped_providers_should_specify_dependencies
severity: Severity.warning
message: Providers which are overridden in a non-root ProviderContainer/ProviderScope should specify dependencies.
test/lints/scoped_providers_should_specify_dependencies.dart:217:11
test/lints/scoped_providers_should_specify_dependencies.dart:172:11

```dart
.overrideWith(() => throw UnimplementedError()),
Expand All @@ -136,7 +91,7 @@ test/lints/scoped_providers_should_specify_dependencies.dart:217:11
code: scoped_providers_should_specify_dependencies
severity: Severity.warning
message: Providers which are overridden in a non-root ProviderContainer/ProviderScope should specify dependencies.
test/lints/scoped_providers_should_specify_dependencies.dart:236:9
test/lints/scoped_providers_should_specify_dependencies.dart:191:9

```dart
.overrideWith(() => throw UnimplementedError()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,6 @@ void main() {
child: Container(),
),
);

flutter.runApp(
ProviderScope(
// ignore: deprecated_member_use
parent: rootContainer,
overrides: [
scopedProvider.overrideWith((ref) => 0),
unimplementedScopedProvider
.overrideWith(() => throw UnimplementedError()),
// expect_lint: scoped_providers_should_specify_dependencies
rootProvider.overrideWith((ref) => 0),
],
child: Container(),
),
);
}

// Regression tests for https://github.com/rrousselGit/riverpod/issues/2340
Expand Down Expand Up @@ -119,21 +104,6 @@ void definitelyNotAMain() {
child: Container(),
),
);

flutter.runApp(
ProviderScope(
// ignore: deprecated_member_use
parent: rootContainer,
overrides: [
scopedProvider.overrideWith((ref) => 0),
unimplementedScopedProvider
.overrideWith(() => throw UnimplementedError()),
// expect_lint: scoped_providers_should_specify_dependencies
rootProvider.overrideWith((ref) => 0),
],
child: Container(),
),
);
}

void someTestFunction() {
Expand All @@ -159,21 +129,6 @@ void someTestFunction() {
),
);

await tester.pumpWidget(
ProviderScope(
// ignore: deprecated_member_use
parent: rootContainer,
overrides: [
scopedProvider.overrideWith((ref) => 0),
unimplementedScopedProvider
.overrideWith(() => throw UnimplementedError()),
// expect_lint: scoped_providers_should_specify_dependencies
rootProvider.overrideWith((ref) => 0),
],
child: Container(),
),
);

await tester.pumpWidget(
Container(
child: ProviderScope(
Expand Down

0 comments on commit e72869e

Please sign in to comment.