Home > Mobile >  A value of type 'Null' can't be assigned to a parameter of type 'List<Color&g
A value of type 'Null' can't be assigned to a parameter of type 'List<Color&g

Time:01-15

I've a widget which accepts an array of colors which I need to use in my widget. I don't understand why it keeps giving me the errors below.

My my widget looks like the following;

 class RoundedGradientButton extends StatelessWidget {

  const RoundedGradientButton({
    required this.gradientColors,
    super.key,
  });

 final List<Color> gradientColors;

  @override
  Widget build(BuildContext context) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(20),
      child: Stack(
        children: <Widget>[
          Positioned.fill(
            child: Container(
              decoration: const BoxDecoration(
                gradient: LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  colors: this.gradientColors // -> Complains
                )
              ),
            ),
          ),
          TextButton(
            style: TextButton.styleFrom(
                foregroundColor: Colors.white,
                padding: const EdgeInsets.only(top: 10, bottom: 10),
                textStyle: const TextStyle(fontSize: 16),
                minimumSize: const Size.fromHeight(0)),
            onPressed: () {},
            child: const Text('Start')
          ),
        ],
      ),
    );
  }
}

Errors

  • A value of type 'Null' can't be assigned to a parameter of type 'List' in a const constructor. Try using a subtype, or removing the keyword 'const'.
  • Invalid constant value.

CodePudding user response:

Remove const here

const BoxDecoration(
                gradient: LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  colors: this.gradientColors // -> Complains
                )

CodePudding user response:

For your first issue it seems you are passing null value when you are using RoundedGradientButton, make sure you are calling it like this:

RoundedGradientButton(gradientColors: [your color]);

for second issue the gradientColors variable is not constant so you need to remove const keyword before BoxDecoration, your full code will be like this:

class RoundedGradientButton extends StatelessWidget {
  const RoundedGradientButton({
    required this.gradientColors,
    super.key,
  });

  final List<Color> gradientColors;

  @override
  Widget build(BuildContext context) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(20),
      child: Stack(
        children: <Widget>[
          Positioned.fill(
            child: Container(
              decoration: BoxDecoration( // remove const from here
                gradient: LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  colors: gradientColors, // change this
                ),
              ),
            ),
          ),
          TextButton(
              style: TextButton.styleFrom(
                  padding: const EdgeInsets.only(top: 10, bottom: 10),
                  textStyle: const TextStyle(fontSize: 16),
                  minimumSize: const Size.fromHeight(0)),
              onPressed: () {},
              child: const Text('Start')),
        ],
      ),
    );
  }
}

CodePudding user response:

The issue is using const and trying to get variable on runtime. If you notice the error message, it included

'List' in a const constructor. Try using a subtype, or removing the keyword 'const'.

Remove const before BoxDecoration

Positioned.fill(
  child: Container(
    decoration: BoxDecoration( //this line `const`
        gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: gradientColors)),
  ),
),
  • Related