hello guys I didn't understand the red line mark coed why it is used in this code. is their any other alternate code we can use instead of that.
CodePudding user response:
CategoryCard class declared icon and name. Using constructor you can initiate it.
Red line code indicates the constructor with values.
CodePudding user response:
Use this code :
class CategoryCard extends StatelessWidget {
final Icon icon;
final String name;
CategoryCard({Key? key,required this.icon,required this.name}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
icon,
Text(name)
],
),
);
}
}
CodePudding user response:
CategoryCard class declared icon and name. Using constructor you can initiate it.
Red line code indicates the constructor with values.
You can make it nullable or required as per your requirement.
For Required ::
class CategoryCard extends StatelessWidget {
final Icon icon;
final String name;
CategoryCard({Key? key,required this.icon,required this.name}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
icon,
Text(name)
],
),
);
}
}
For Nullable ::
class CategoryCard extends StatelessWidget {
final Icon? icon;
final String? name;
CategoryCard({Key? key,this.icon,this.name}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
icon!,
Text(name!)
],
),
);
}
}
