u2tall
u2tall

Reputation: 490

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,
         ),

Upvotes: 14

Views: 10093

Answers (3)

MAHAN_vs_MAHAN
MAHAN_vs_MAHAN

Reputation: 1

here is my ThemeData :

theme: ThemeData(
      // This is the theme of your application.
      //
      // TRY THIS: Try running your application with "flutter run". You'll see
      // the application has a purple toolbar. Then, without quitting the app,
      // try changing the seedColor in the colorScheme below to Colors.green
      // and then invoke "hot reload" (save your changes or press the "hot
      // reload" button in a Flutter-supported IDE, or press "r" if you used
      // the command line to start the app).
      //
      // Notice that the counter didn't reset back to zero; the application
      // state is not lost during the reload. To reset the state, use hot
      // restart instead.
      //
      // This works for code too, not just values: Most code changes can be
      // tested with just a hot reload.
      colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
      brightness: Brightness.dark,
      textTheme: GoogleFonts.latoTextTheme(),
      useMaterial3: true,

I had no problem until I added brightness: Brightness.dark. after that i got this error in my app in android emulator:

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

I solved this problem by removing this line from my ThemeData Source code :

colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),

Upvotes: 0

Omar Essam El-Din
Omar Essam El-Din

Reputation: 1873

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

Upvotes: 3

BJW
BJW

Reputation: 1095

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),
          ),

Upvotes: 32

Related Questions