Skip to content

Commit f5fb9e8

Browse files
authored
Fix ThemeData extension throws when the ThemeExtension not found (#103343)
1 parent a259fcb commit f5fb9e8

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

packages/flutter/lib/src/material/theme_data.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ class ThemeData with Diagnosticable {
10771077
/// Obtain with `Theme.of(context).extension<MyThemeExtension>()`.
10781078
///
10791079
/// See [extensions] for an interactive example.
1080-
T? extension<T>() => extensions[T] as T;
1080+
T? extension<T>() => extensions[T] as T?;
10811081

10821082
/// The default [InputDecoration] values for [InputDecorator], [TextField],
10831083
/// and [TextFormField] are based on this theme.

packages/flutter/test/material/theme_data_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,14 @@ void main() {
561561
expect(lerped.extension<MyThemeExtensionA>()!.color2, const Color(0xff90ab7d));
562562
expect(lerped.extension<MyThemeExtensionB>()!.textStyle, const TextStyle(fontSize: 100)); // Not lerped
563563
});
564+
565+
testWidgets('should return null on extension not found', (WidgetTester tester) async {
566+
final ThemeData theme = ThemeData(
567+
extensions: const <ThemeExtension<dynamic>>{},
568+
);
569+
570+
expect(theme.extension<MyThemeExtensionA>(), isNull);
571+
});
564572
});
565573

566574
test('copyWith, ==, hashCode basics', () {

0 commit comments

Comments
 (0)