Flutter ,dart,The parameter 'colour' can't have a value of 'null' because of its type, but the implicit default value is 'null'

414

As said also in the previous anwser:

  1. Change the @required => required
  2. Add '?' to potentially nullable variable
  3. Add default value

In your case maybe I'll use something like this:

final String title;
final Color colour;
final VoidCallBack? onPressed;

RoundedButton({
    required this.title,
    this.colour = Colors.blue,
    this.onPressed,
});

Because the title can't be null, the colour can be the default one and the onPress can be empty for future implementation of your app.

Share:
414
Kevlar
Author by

Kevlar

Updated on December 19, 2022

Comments

  • Kevlar
    Kevlar over 1 year

    i dont know why i am getting the errors,i have previously coded the same and it worked perfectly fine maybe some new update or plugin please help me solve my error

    import 'package:flutter/material.dart';
    
    class RoundedButton extends StatelessWidget {
      final String title;
      final Color colour;
      final Function onPressed;
      RoundedButton({this.title, this.colour, @required this.onPressed});
    
      @override
      Widget build(BuildContext context) {
        return Padding(
          padding: EdgeInsets.symmetric(vertical: 16.0),
          child: Material(
            elevation: 5.0,
            color: colour,
            borderRadius: BorderRadius.circular(30.0),
            child: MaterialButton(
              onPressed: onPressed,
              minWidth: 200.0,
              height: 42.0,
              child: Text(
                title,
                style: TextStyle(color: Colors.white),
              ),
            ),
          ),
        );
      }
    }
    

    enter image description here