希望你們做得好..
所以,早些時候我在 Youtube 上關注 Flutter 教程,看起來他的 BorderBox 類沒有問題,但我的不同,它有 3 個問題導致填充,寬度,高度,為空。
class BorderBox extends StatelessWidget {
final Widget child;
final EdgeInsets padding;
final double width, height;
const BorderBox({Key? key,
this.padding,
this.width,
this.height,
required this.child}) : super(key: key);
帶有以下錯誤訊息
引數 'padding' 的值不能為 'null',因為它的型別,但隱含的默認值是 'null'。
引數 'width' 的值不能為 'null',因為它的型別,但隱含的默認值是 'null'。
引數 'height' 的值不能為 'null',因為它的型別,但隱含的默認值是 'null'。
有沒有辦法繞過空引數?這也是我在 youtube 上遵循教程的視頻Here
謝謝你,有一個美好的一天...
uj5u.com熱心網友回復:
required為不可為空的值添加建構式,如下所示:
const BorderBox({
Key? key,
required this.padding,
required this.width,
required this.height,
required this.child,
}) : super(key: key);
但是,如果您不希望它們被要求,您可以像這樣使它們為空:
class BorderBox extends StatelessWidget {
final Widget child;
final EdgeInsets? padding;
final double? width, height;
const BorderBox({
Key? key,
this.padding,
this.width,
this.height,
required this.child,
}) : super(key: key);
這 ?意味著該變數可以為空,這使您能夠不使其成為必需。
您可以在此處閱讀有關 null 安全性的更多資訊:https ://dart.dev/null-safety
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/468623.html
上一篇:從子類Flutter激活函式
