Open longtimedeveloper opened 3 years ago
The parameter 'cardChild' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
How to use widgets and objects inside a class without instantiating it such that by default value of that widget is not null
Hey, even I was struggling with the same problem, and I somehow managed to solve it:
Actually you have to just add a ?
. Here is how:
class BaseCard extends StatelessWidget {
BaseCard( {required Color color, Widget? cardChild} ){
this.color = color;
this.cardChild = cardChild;
}
late Color color;
late Widget? cardChild;
notice there is a ?
after Widget in
BaseCard( {required Color color, Widget? cardChild} )
, this says that cardChild is nullable
(* you have to put ?
after Widget during initialising also late Widget? cardChild;
)
i hope it was help full
Section 12 Code Shown by Instructor will not work when Null Safety is enabled.
This section requires updating. Students not familiar with Null Safety will be in a bad place of frustration.