Skip to content

Commit

Permalink
Fix no rebuild when listening to context of Selector.builder (#863)
Browse files Browse the repository at this point in the history
Fixes #339
  • Loading branch information
spkersten authored Feb 28, 2024
1 parent c4b6c2b commit 650e6a5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/provider/lib/src/selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ class _Selector0State<T> extends SingleChildState<Selector0<T>> {
if (shouldInvalidateCache) {
value = selected;
oldWidget = widget;
cache = widget.builder(
context,
selected,
child,
cache = Builder(
builder: (context) => widget.builder(
context,
selected,
child,
),
);
}
return cache!;
Expand Down
50 changes: 50 additions & 0 deletions packages/provider/test/null_safe/selector_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,36 @@ void main() {
expect(find.text('24'), findsOneWidget);
});

testWidgets('rebuild when inherited widget changes', (tester) async {
final selectorWidget = Directionality(
textDirection: TextDirection.ltr,
child: Selector0<int>(
selector: (_) => 0,
builder: (context, _, __) =>
Text('${DummyInheritedWidget.of(context).data}'),
),
);

await tester.pumpWidget(
DummyInheritedWidget(
data: 1,
child: selectorWidget,
),
);

expect(find.text('1'), findsOneWidget);

await tester.pumpWidget(
DummyInheritedWidget(
data: 2,
child: selectorWidget,
),
);

expect(find.text('1'), findsNothing);
expect(find.text('2'), findsOneWidget);
});

testWidgets('debugFillProperties', (tester) async {
final builder = DiagnosticPropertiesBuilder();
final key = UniqueKey();
Expand Down Expand Up @@ -524,3 +554,23 @@ class MockBuilder<T> extends Mock {
) as Widget;
}
}

class DummyInheritedWidget extends InheritedWidget {
const DummyInheritedWidget({
required this.data,
required Widget child,
Key? key,
}) : super(child: child, key: key);

final int data;

static DummyInheritedWidget of(BuildContext context) {
final result =
context.dependOnInheritedWidgetOfExactType<DummyInheritedWidget>();
assert(result != null, 'No DummyInheritedWidget found in context');
return result!;
}

@override
bool updateShouldNotify(DummyInheritedWidget old) => old.data != data;
}

0 comments on commit 650e6a5

Please sign in to comment.