I try to set default value in function:
bool isOnGoing([DateTime date = DateTime.now()]) {
...
}
But studio returns "Default values of an optional parameter must be constant".
How can I set default parameter in this case?
The syntax you use is correct, but as the error message says, the value has to be a compile time constant.
A compile time constant doesn't make sense for DateTime.now()
.
As a workaround, you can use:
/// Returns `true` is still going on.
///
/// [date] the date to check.
/// as default value `DateTime.now()` is used
/// if no value or `null` was passed.
bool isOnGoing([DateTime date]) {
date ??= DateTime.now();
...
}
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras