londonappbrewery / bmi-calculator-flutter

Learn to Code While Building Apps - The Complete Flutter Development Bootcamp
https://www.appbrewery.co
192 stars 837 forks source link

Section 12 Code Shown by Instructor will not work when Null Safety is enabled. #20

Open longtimedeveloper opened 3 years ago

longtimedeveloper commented 3 years ago

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.

Lakhankumawat commented 3 years ago

The parameter 'cardChild' can't have a value of 'null' because of its type, but the implicit default value is 'null'.

Lakhankumawat commented 3 years ago

How to use widgets and objects inside a class without instantiating it such that by default value of that widget is not null

vinayak5002 commented 3 years ago

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