Flutter 我不断得到';空';在控制台打印内容的末尾

Flutter 我不断得到';空';在控制台打印内容的末尾,flutter,dart,Flutter,Dart,我使用的是DartPad,其他if-else语句和循环也出现了这种情况,我不知道为什么。我试着重新设置键盘,但没用,有什么想法吗 这是控制台打印的内容: 单词板是一个等值线! 空的 代码如下: void main (){ print (isIsogram('board')); } isIsogram (String word){ var splitWord = word.split('').toSet(); if (splitWord.length == word.length)

我使用的是DartPad,其他if-else语句和循环也出现了这种情况,我不知道为什么。我试着重新设置键盘,但没用,有什么想法吗

这是控制台打印的内容:

单词板是一个等值线! 空的

代码如下:

void main (){
  print (isIsogram('board'));
}

isIsogram (String word){
  var splitWord = word.split('').toSet();
  if (splitWord.length == word.length) {
    print ('The word $word is an isogram!');
  }
  else {
    print ('The word $word is not an isogram');  
  }
}

isIsogram
必须具有返回类型以避免打印
null

void main (){
  print (isIsogram('board'));
}

String isIsogram (String word){
  var splitWord = word.split('').toSet();
  if (splitWord.length == word.length) {
    print ('The word $word is an isogram!');
  }
  else {
    print ('The word $word is not an isogram');  
  }
  return word;
}
输出


@fay,您只需在
main()
中删除打印,因为它已经在
isIsogram

void main (){
  isIsogram('board');
}

isIsogram (String word){
  var splitWord = word.split('').toSet();
  if (splitWord.length == word.length) {
    print ('The word $word is an isogram!');
  }
  else {
    print ('The word $word is not an isogram');  
  }
}
截图:


技术上的解释是,Dart中所有未标记为
void
的方法都必须返回某些内容,并且当没有任何内容被
返回X显式返回时null
。同样,当您
打印不返回任何内容的非void方法的返回值时,您最终打印的是隐式返回的
null
值。@Abion47,您是正确的。我假设OP只是通过打印进行测试,并试图找出他的逻辑是否不正确。我的意图是让他知道他打印了两次,但我知道这可能需要一些解释。这个答案解决了打印
null
的问题,但没有解释为什么它首先打印
null
。我在回答中给出了一个提示。因为
isIsogram
没有返回类型,所以每当调用此函数并返回到
print(isIsogram('board'))它打印空值。但它没有解释为什么它打印空值。它只是说是的。我在我对被接受答案的评论中贴出了一条解释。是的,我读到了!多亏了你,用更专业的话来说:)
void main (){
  print (isIsogram('board'));
   }

 isIsogram (String word){
  var splitWord = word.split('').toSet();
  if (splitWord.length != word.length) {
    print ('The word $word is an isogram!');
  }
  else {
    print ('The word $word is not an isogram');  
  }
  return word;
}