I trying to make a light theme and a dark theme for my app, I making an course of flutter and the code it's a little out of date and some commands doesn't work in the actual version of Flutter, in special the Themedata.accentColor
or just accentColor
who is deprecated in the last Flutter version.
I tried to search for a solution and I found two of them, one in the Flutter docs and other on another question of StackOverflow. I tried both of solutions but I still getting the same error
Error message:
Exception has occurred.
_AssertionError ('package:flutter/src/material/theme_data.dart': Failed assertion: line 412 pos 12: 'colorScheme?.brightness == null || brightness == null || colorScheme!.brightness == brightness': is not true.)
Code:
colorScheme: ColorScheme.fromSwatch().copyWith(
secondary: Colors.white,
),
My code is:
import 'package:flutter/material.dart';
const brightness = Brightness.dark;
Color primaryColor = const Color(0xFF00C569);
Color lightColor = const Color(0xFFFFFFFF);
Color backgroundColor = const Color(0xFFF5F5F5);
Color dangerColor = const Color(0xFFFF0000);
ThemeData darkTheme() {
return ThemeData(
brightness: brightness,
//iconTheme: const IconThemeData(color: Colors.black),
//textTheme: const TextTheme(
/*
bodyText2: TextStyle(color: Colors.red),
headline1: TextStyle(fontSize: 78),
headline2: TextStyle(
color: Colors.black,
fontSize: 30,
),
button: TextStyle(color: Colors.green),
),
*/
colorScheme: ColorScheme.fromSwatch().copyWith(
secondary: Colors.white,
),
primaryColor: primaryColor,
);
}
I actually discovered a temporary solution for this.
The problem was a possible null value, so the code don't work. The solution basically put a ?
after the ColorScheme.
For example:
colorScheme: ColorScheme?.fromSwatch().copyWith(
secondary: Colors.white,
),
if still doesn't work, try to add the brightness in the color scheme, for example:
colorScheme: ColorScheme?.fromSwatch().copyWith(
brightness: Brightness.light,
secondary: Colors.white,
),