Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flutter_bloc: Fix provider exception #1286

Merged
merged 1 commit into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/flutter_bloc/lib/src/bloc_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ class BlocProvider<T extends Bloc<dynamic, dynamic>>
static T of<T extends Bloc<dynamic, dynamic>>(BuildContext context) {
try {
return Provider.of<T>(context, listen: false);
} on ProviderNotFoundException catch (_) {
} on ProviderNotFoundException catch (e) {
if (e.valueType != T) rethrow;
throw FlutterError(
"""
BlocProvider.of() called with a context that does not contain a Bloc of type $T.
Expand Down
18 changes: 16 additions & 2 deletions packages/flutter_bloc/test/bloc_provider_test.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart';

class MyApp extends StatelessWidget {
final CounterBloc Function(BuildContext context) _create;
Expand Down Expand Up @@ -436,6 +436,20 @@ void main() {
expect(exception.message, expectedMessage);
});

testWidgets(
'should not wrap into FlutterError if '
'ProviderNotFoundException with wrong valueType '
'is thrown', (tester) async {
await tester.pumpWidget(
BlocProvider<CounterBloc>(
create: (context) => CounterBloc(onClose: Provider.of(context)),
child: CounterPage(),
),
);
final dynamic exception = tester.takeException();
expect(exception is ProviderNotFoundException, true);
});

testWidgets(
'should not throw FlutterError if internal '
'exception is thrown', (tester) async {
Expand Down