Fix ThemeData extension throws when the ThemeExtension not found (#103343)

This commit is contained in:
Andree Yosua
2022-05-11 11:44:07 +07:00
committed by GitHub
parent a259fcb64e
commit f5fb9e8c01
2 changed files with 9 additions and 1 deletions

View File

@@ -1077,7 +1077,7 @@ class ThemeData with Diagnosticable {
/// Obtain with `Theme.of(context).extension<MyThemeExtension>()`.
///
/// See [extensions] for an interactive example.
T? extension<T>() => extensions[T] as T;
T? extension<T>() => extensions[T] as T?;
/// The default [InputDecoration] values for [InputDecorator], [TextField],
/// and [TextFormField] are based on this theme.

View File

@@ -561,6 +561,14 @@ void main() {
expect(lerped.extension<MyThemeExtensionA>()!.color2, const Color(0xff90ab7d));
expect(lerped.extension<MyThemeExtensionB>()!.textStyle, const TextStyle(fontSize: 100)); // Not lerped
});
testWidgets('should return null on extension not found', (WidgetTester tester) async {
final ThemeData theme = ThemeData(
extensions: const <ThemeExtension<dynamic>>{},
);
expect(theme.extension<MyThemeExtensionA>(), isNull);
});
});
test('copyWith, ==, hashCode basics', () {