Skip to content

ThemeData extension method throw exception when the ThemeExtension is null #103313

@SuperPenguin

Description

@SuperPenguin

Steps to Reproduce

  1. Run code below
  2. Change themeMode to dark

Expected results: No Exception

Actual results: Exception

Code sample
import 'package:flutter/material.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  runApp(const App());
}

class App extends StatelessWidget {
  const App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      themeMode: ThemeMode.light,
      theme: ThemeData(
        brightness: Brightness.light,
        extensions: {
          const MyTheme(
            heading47: TextStyle(
              color: Colors.red,
            ),
          ),
        },
      ),
      darkTheme: ThemeData(
        brightness: Brightness.dark,
      ),
      home: const Home(),
    );
  }
}

class Home extends StatelessWidget {
  const Home({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Title'),
      ),
      body: Center(
        child: Text(
          'Heading47',
          style: MyTheme.maybeOf(context)?.heading47,
        ),
      ),
    );
  }
}

@immutable
class MyTheme extends ThemeExtension<MyTheme> {
  const MyTheme({
    required this.heading47,
  });

  final TextStyle heading47;

  @override
  ThemeExtension<MyTheme> copyWith({
    TextStyle? heading47,
  }) {
    return MyTheme(heading47: heading47 ?? this.heading47);
  }

  @override
  ThemeExtension<MyTheme> lerp(
    ThemeExtension<MyTheme>? other,
    double t,
  ) {
    if (other is! MyTheme) return this;

    return MyTheme(
      heading47: TextStyle.lerp(heading47, other.heading47, t)!,
    );
  }
  
  @override
  bool operator ==(Object other) {
    if (other is! MyTheme) return false;

    return other.heading47 == heading47;
  }

  @override
  int get hashCode => Object.hashAll([heading47]);

  static MyTheme? maybeOf(BuildContext context) {
    return Theme.of(context).extension<MyTheme>();
  }

  static MyTheme of(BuildContext context) {
    return Theme.of(context).extension<MyTheme>()!;
  }
}
Logs
_CastError (type 'Null' is not a subtype of type 'MyTheme' in type cast)
[√] Flutter (Channel beta, 2.13.0-0.4.pre, on Microsoft Windows [Version
    10.0.22000.613], locale en-ID)
    • Flutter version 2.13.0-0.4.pre at C:\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 25caf1461b (3 days ago), 2022-05-05 14:23:09
      -0700
    • Engine revision c5caf749fe
    • Dart version 2.17.0 (build 2.17.0-266.8.beta)
    • DevTools version 2.12.2

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at C:\Users\andre\AppData\Local\Android\sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.11+9-b60-7590822)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.9)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.11.32106.194
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2021.1)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.11+9-b60-7590822)

[√] VS Code (version 1.67.0)
    • VS Code at C:\Users\andre\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.40.0

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.22000.613]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 100.0.4896.127
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 101.0.1210.39

[√] HTTP Host Availability
    • All required HTTP hosts are available

T? extension<T>() => extensions[T] as T;

This cast should be changed to as T? instead

Metadata

Metadata

Assignees

No one assigned

    Labels

    f: material designflutter/packages/flutter/material repository.found in release: 2.13Found to occur in 2.13frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer version

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions