這是我想改變的功能
這是功能
Expanded buildKey({Color bgColor, int n}) {
return Expanded(
child: TextButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(bgColor),
),
onPressed: () {
playSound(n);
},
child: Text(''),
),
);
}
在這里我呼叫上面的函式
buildKey(Color: Colors.red, n: 1);
我收到的這 2 個錯誤。
錯誤:引數“n”因其型別而不能具有“null”值,但隱式默認值為“null”。(missing_default_value_for_parameter at [xylophone] lib\main.dart:12)
錯誤:引數“bgColor”因其型別而不能具有“null”值,但隱式默認值為“null”。(missing_default_value_for_parameter at [xylophone] lib\main.dart:12)
uj5u.com熱心網友回復:
發生這種情況是因為您傳遞的引數可以為空,因此為它們提供如下所需的屬性。
Expanded buildKey({required Color bgColor, required int n}) {
return Expanded(
child: TextButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(bgColor),
),
onPressed: () {
playSound(n);
},
child: Text(''),
),
);
}
uj5u.com熱心網友回復:
Expanded buildKey({Color bgColor, int n}) {
return Expanded(
child: TextButton(
style: ButtonStyle(
backgroundColor: bgColor != null ? MaterialStateProperty.all(bgColor) : MaterialStateProperty.all(Colors.green),
),
onPressed: () {
playSound(n);
},
child: Text(''),
),
);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/400874.html
