Issue
code here constructor's this.colour vairable does not work
class ReusableCard extends StatelessWidget {
Color colour= null;
ReusableCard({@required this.colour});
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.all(15),
decoration: BoxDecoration(
color: colour,
borderRadius: BorderRadius.circular(10.0),
),
);
}
}
Solution
This code should solve your problem:
ReusableCard({this.colour});
Color? colour;
Because Color isnt assigned yet it can be null. Dart has nullsafety so the question mark implies the value can be null. See this link for more details: https://sanjibsinha.com/null-safety-in-flutter-dart/
Edit: you could also do it like this:
ReusableCard({this.colour});
late Color colour;
The late keyword to initialize a variable when it is first read, rather than when it's created
Answered By - EnviroApps
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.