英文:
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<String> myKeywords = [];
if(myKeywords.isNotEmpty){
print("Keywords is not empty..?");
print("Keywords length = ${myKeywords.length}");
print("Keyword: \"${myKeywords.first}\"");
}
// output
Keywords is not empty..?
Keywords length = 0
// Error: Bad state: No element
This doesn't work the same way with strings:
String myString = "";
if(myString.isNotEmpty){
print("myString is not empty..?");
print("myString length = ${myString.length}");
}
else{
print("myString is empty");
}
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<String> myKeywords = [];
if (myKeywords.isNotEmpty){
print("Keywords is not empty..?");
print("Keywords length = ${myKeywords.length}");
print("Keyword: \"${myKeywords.first}\"");
} else {
print("Keywords is empty");
}
以下代码应返回 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<String> myKeywords = [];
if (myKeywords.isNotEmpty){
print("Keywords is not empty..?");
print("Keywords length = ${myKeywords.length}");
print("Keyword: \"${myKeywords.first}\"");
} else {
print("Keywords is empty");
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论