我刚开始使用dart (来自python背景),而且我正在努力使函数正常工作。我创建了以下函数:
void main() {
int number = 22;
Function evenChecker(int number) {
if ((number%2) == 0) {
print('$number is even');
} else {
print('$number is odd');
}
}
}但是得到以下错误:
Error: A non-null value must be returned since the return type 'Function' doesn't allow null.
bin/hello_dart_project.dart:4
- 'Function' is from 'dart:core'.此外,如果有人对一个好的飞镖学习资源有任何建议,我将非常感谢您的建议。^
发布于 2022-06-29 23:49:16
与Python或JavaScript不同的是,在dart中没有定义函数的关键字。相反,我们以函数的返回类型开始声明函数:
void evenChecker(...) {...}
^^^^
// This is the return type. "void" means "returns the value null" or "doesn't return anything"https://stackoverflow.com/questions/72804390
复制相似问题