Mohnish Sharma
Mohnish Sharma

Reputation: 11

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

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),
      ),
    );
  }
}

Upvotes: 1

Views: 37

Answers (1)

Ivo
Ivo

Reputation: 23144

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;

Upvotes: 1

Related Questions