How can I use both required and simple argument in same constructor in flutter 3?

Viewed 35
class ReusableCard extends StatelessWidget {
  ReusableCard({required this.colour, this.cardChild});

  final Color colour;
  Widget cardChild;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: cardChild,
      margin: EdgeInsets.all(15.0),
      decoration: BoxDecoration(
        color: Color(0xFF1D1E33),
        borderRadius: BorderRadius.circular(10.0),
      ),
    );
  }
}
1 Answers

If you don't want it required you need to make it nullable by adding a ? to the type of the variable. So change

Widget cardChild;

to

Widget? cardChild;
Related