Sta*_*kov 5 dart flutter dart-null-safety
我目前正在学习 Flutter 的课程。他们为您提供了一个旧的存根项目(可为空)。当我尝试迁移到 Flutter 2.12.0 或更高版本时 - null 安全性启动。我对其工作原理有基本的了解 - 但我在 Google 或 StackOverFlow 上找不到任何地方。
我有一个自定义卡片小部件。它需要一个颜色参数。另外,我需要它能够接收 Widget 子参数以及 onPress 函数,但我不想让它们成为必需的。请帮忙。如何为小组件和函数创建默认值?
class ReusableCard extends StatelessWidget {
ReusableCard({@required this.cardColor, this.cardChild, this.onPress});
final Color cardColor;
final Widget cardChild;
final Function onPress;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onPress,
child: Container(
child: cardChild,
margin: EdgeInsets.all(10.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
color: cardColor,
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以将它们设置为可为空字段,如下所示
ReusableCard({required this.cardColor, this.cardChild, this.onPress});
final Color cardColor;
final Widget? cardChild;
final VoidCallback? onPress;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5124 次 |
| 最近记录: |