Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting failed assertion with setting brightness: Brightness.dark for darkTheme

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,
         ),
like image 346
u2tall Avatar asked Feb 01 '26 20:02

u2tall


2 Answers

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),
          ),
like image 76
BJW Avatar answered Feb 04 '26 10:02

BJW


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
like image 42
Omar Essam Avatar answered Feb 04 '26 10:02

Omar Essam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!