Const variables must be initialized with a constant value

Viewed 3903

When I do something like this

import 'package:flutter/material.dart';

const kBaseColor = Colors.deepPurple;
const kBase300 = Colors.deepPurple[300]; // there is an error

error: Const variables must be initialized with a constant value. (const_initialized_with_non_constant_value at [myprogect] lib\ui\widgets\constants.dart:4)

Isn't it possible to assign a shade of the color as const variable?

1 Answers

you can use final instead of that, the reason is If the value you have is computed at runtime you can not use a const for it. you can use final instead of that like this

final kBase300 = Colors.deepPurple[300];
Related