Dart/Flutter中List.isNotEmpty()是否不正确?

huangapple go评论132阅读模式
英文:

Dart/Flutter List.isNotEmpty() is incorrect?

问题

我试图访问一个列表,但只有在它不为空的情况下。在收到一些奇怪的路径后,我发现List.isNotEmpty返回的长度为0,而它并不是空的。除非,当然,我所认为的“空”是错误的心态?

这是我运行的测试,我创建了一个“空”列表。然后,我测试了isNotEmpty返回的内容。

List<String> myKeywords = [];
if(myKeywords.isNotEmpty){
  print("Keywords is not empty..?");
  print("Keywords length = ${myKeywords.length}");
  print("Keyword: \"${myKeywords.first}\"");
}

// 输出

Keywords is not empty..?
Keywords length = 0
// Error: Bad state: No element

这在字符串中不起作用:

String myString = "";
if(myString.isNotEmpty){
  print("myString is not empty..?");
  print("myString length = ${myString.length}");
}
else{
  print("myString is empty");
}

输出:

myString is empty

查看文档,List.isNotEmpty说明如下:

Whether this collection has at least one element.
May be computed by checking if iterator.moveNext() returns true.
Example:
final numbers = {1, 2, 3};
print(numbers.isNotEmpty); // true;
print(numbers.iterator.moveNext()); // true

有人能解释一下吗?或者,也许这是一个错误?

Android Studio Dart SDK 版本 - 2.19.4 (稳定) Flutter 版本 - 3.7.7 (稳定)

谢谢!

英文:

I'm trying to access a list, but only if it isn't empty. After receiving some odd pathing, I discovered that the List.isNotEmpty is returning a length of 0, and it is not empty. Unless, of course, what I consider "empty" is in the wrong mindset?

Here's the test I ran, where I created an "empty" list. Then, I tested to see what came back when I tested isNotEmpty.

List&lt;String&gt; myKeywords = [];
if(myKeywords.isNotEmpty){
  print(&quot;Keywords is not empty..?&quot;);
  print(&quot;Keywords length = ${myKeywords.length}&quot;);
  print(&quot;Keyword: \&quot;${myKeywords.first}\&quot;&quot;);
}

// output

Keywords is not empty..?
Keywords length = 0
// Error: Bad state: No element

This doesn't work the same way with strings:

String myString = &quot;&quot;;
if(myString.isNotEmpty){
  print(&quot;myString is not empty..?&quot;);
  print(&quot;myString length = ${myString.length}&quot;);
}
else{
  print(&quot;myString is empty&quot;);
}

Output:

myString is empty

Looking at documentation, List.isNotEmpty states the following:

Whether this collection has at least one element.
May be computed by checking if iterator.moveNext() returns true.
Example:
final numbers =  {1, 2, 3};
print(numbers.isNotEmpty); // true;
print(numbers.iterator.moveNext()); // true

Can someone explain this to me? Or, perhaps this is a bug?

Android Studio
Dart SDK Version - 2.19.4 (Stable)
Flutter Version - 3.7.7 (Stable)

Thank you!

答案1

得分: 5

它实际上按预期工作。尝试调试您的程序,因为上面给出的输出是不正确的。

确保在isNotEmpty和初始化之间没有添加任何元素。

以下代码的输出将是:

List&lt;String&gt; myKeywords = [];
if (myKeywords.isNotEmpty){
  print(&quot;Keywords is not empty..?&quot;);
  print(&quot;Keywords length = ${myKeywords.length}&quot;);
  print(&quot;Keyword: \&quot;${myKeywords.first}\&quot;&quot;);
} else {
  print(&quot;Keywords is empty&quot;);
}

以下代码应返回 Keywords is empty

检查此 DartPad 链接。

英文:

It is actually working as expected. Try debugging your program, because the output stated above is incorrect

Make sure you have not added any element between isNotEmpty and initialization.

The output of the following code would be:

List&lt;String&gt; myKeywords = [];
if (myKeywords.isNotEmpty){
  print(&quot;Keywords is not empty..?&quot;);
  print(&quot;Keywords length = ${myKeywords.length}&quot;);
  print(&quot;Keyword: \&quot;${myKeywords.first}\&quot;&quot;);
} else {
  print(&quot;Keywords is empty&quot;);
}

The following code should return Keywords is empty.

Check this DartPad link.

答案2

得分: 1

我觉得Flutter有一些问题。我在DartPad中测试过,它运行正常。

DartPad链接在这里。

我建议你在Github的Flutter存储库中开一个问题。

英文:

I think there are some issues with Flutter. I tested it in DartPad and it works fine.

Here is the DartPad link.

I suggest you open an issue in the Flutter repository on Github.

huangapple
  • 本文由 发表于 2023年3月12日 16:31:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定