12

I'm getting this error:

'package:flutter/src/material/theme_data.dart': Failed assertion: line 412 pos 12: 'colorScheme?.brightness == null || brightness == null || colorScheme!.brightness == brightness': is not true.

I've used this brightness: Brightness.dark parameter for my dark mode without any problems until a recent update. I updated several things at once, so I'm not sure what caused the change. Do I need to be setting up my dark mode differently now?

Current dark theme:

darkTheme: ThemeData(
           toggleableActiveColor: Colors.blue,
           visualDensity: VisualDensity.adaptivePlatformDensity,
           textTheme: _textTheme(),
           colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.blue).copyWith(secondary: Colors.blueAccent),
           brightness: Brightness.dark,
         ),

2 Answers 2

23

This is a consequence of tightening up the ThemeData constructor wrt the brightness parameter and the ColorScheme's brightness parameter in an update of Flutter. In your example the brightness of the ColorScheme is light (the default), but the ThemeData's brightness is dark.

To get your darkTheme working, you need to remove the brightness parameter and put that in the colorScheme, like so:

darkTheme: ThemeData(
            toggleableActiveColor: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
            colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.blue)
                .copyWith(
                    secondary: Colors.blueAccent, brightness: Brightness.dark),
          ),
1
  • 3
    Thank you! you're my hero
    – u2tall
    Feb 10, 2022 at 18:54
2

Just Add the brightness property in fromSwatch Constructor

in Dark Theme

 colorScheme: ColorScheme.fromSwatch().copyWith(secondary: Colors.blue, 
              brightness: Brightness.dark)

in Light Theme

 brightness: Brightness.light

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.