Dart:警告“信息:此函数的返回类型为'int',但不以return语句结尾””) [英] Dart: warning "info: This function has a return type of 'int', but doesn't end with a return statement")
问题描述
我对以下代码段发出警告,但我不知道为什么。
I got a warning on the following piece of code, and I don't know why.
List<Map<String, int>> callNoInfo = [];
int getCallNo(String phoneNo) {
callNoInfo.forEach((item) {
if (item.containsKey(phoneNo)) {
return item[phoneNo];
}
});
return 0;
}
警告是:
This function has a return type of 'int', but doesn't end with a return statement. (missing_return at [project_name] lib\helper\call_no.dart:35)
谁能告诉我为什么会这样?预先感谢
Can anyone tell me why this happens? thanks in advance
推荐答案
在 forEach
方法中,您正在创建一个lambda函数没有显式定义返回类型,因此Dart试图从 return
语句中推断出它。如果我们从 forEach
方法中拉出函数,则可能有助于了解我的意思:
In the forEach
method, you are creating a lambda function without explicitly defining the return type, so Dart is attempting to infer it from the return
statements. If we pull the function out of the forEach
method, it might help to see what I mean:
...
(item) {
if (item.containsKey(phoneNo)) {
return item[phoneNo];
}
}
...
该函数包含一个 return
语句返回 item [phoneNo]
,这是 int
值。 Dart使用此函数推断此lambda函数的返回类型为 int
。但是,既然知道了这一点,它还会注意到,如果代码执行未输入 if
块,就不会有 return
语句与 if
条件的 else
端匹配。如果 item
对象不包含键 phoneNo
,返回的方法是什么?
The function includes a return
statement that returns item[phoneNo]
, which is an int
value. Using this, Dart infers that the return type of this lambda function is int
. However, now that it knows this, it also notices that if the code execution does not enter the if
block, there is no return
statement to match the else
side of the if
condition. If the item
object does not contain the key phoneNo
, what is the method going to return?
(答案是该方法将隐式返回 null
,这就是为什么消息只是一个警告而不是编译器错误,但出现警告是因为开发人员可能无意这样做,同时也是为了帮助您使代码减少对隐形Dart运行时魔术的依赖。)
(The answer is that the method will implicitly return null
which is why the message is only a warning and not a compiler error, but the warning appears because this probably wasn't intentional by you the developer and also as a nudge to help you make your code less reliant on invisible Dart runtime magicks.)
要解决此问题,在之外,还需要另外一个
返回
>块:
To fix this, there needs to be another return
outside of the if
block:
...
(item) {
if (item.containsKey(phoneNo)) {
return item[phoneNo];
}
return 0;
}
...
但是,现在存在另一个问题。列表中的 forEach
方法具有以下签名:
However, now there's a different problem. The forEach
method on lists has the following signature:
forEach(void f(E element)) → void
实际上,存在两个问题。首先,作为参数传递的方法需要具有 void
的返回类型,而 forEach
方法本身也具有返回类型为 void
。这意味着您根本无法从 forEach
方法中返回值。
In fact, there are two problems. First, the method passed as the parameter needs to have a return type of void
, and the forEach
method itself also has a return type of void
. This means that you cannot return values from within the forEach
method at all.
关于 forEach
方法的事情是,它旨在遍历集合并处理其中的每个值。这并不意味着(也无法)搜索值并在找到后将其返回。此外,迭代是详尽无遗的,这意味着一旦启动迭代,就无法停止该方法,直到对集合中的每个元素都进行了迭代。
The thing about the forEach
method is that it is intended to iterate over the collection and process each of the values within it. It's not meant to (and can't) search for a value and return it once it's found. Furthermore, the iteration is exhaustive, meaning once you start it, the method cannot be stopped until each and every element in the collection has been iterated over.
这是为什么,正如其他答案所指出的那样,您真正应该做的是在循环中使用
或
/ p>
Which is why, as the other answers have pointed out, what you really should be doing is using a for
or for in
loop:
List<Map<String, int>> callNoInfo = [];
int getCallNo(String phoneNo) {
for(var item in callNoInfo) {
if (item.containsKey(phoneNo)) {
return item[phoneNo];
}
}
return 0;
}
(我不确定您为什么没有得到向 forEach
方法分配返回值为 int
的lambda函数的编译器错误,该方法显然要求使用 void
返回类型。但是,如果我不得不猜测,我会说Dart运行时将它们视为兼容,并通过简单地舍弃的返回值来调和返回类型的差异。 lambda函数。)
(I'm not sure why you don't get a compiler error for assigning a lambda function with a return value of int
to the forEach
method which clearly is requesting a one with a void
return type. But if I had to guess, I'd say the Dart runtime treats them as compatible and reconciles the difference in return type by simply discarding the return value of the lambda function.)
这篇关于Dart:警告“信息:此函数的返回类型为'int',但不以return语句结尾””)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!