Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Making Boolean variable in dart. I don't know what I missed

I want to make a variable to check a word that has contained some letters.
But I saw a null-safety error that says ‘The body might complete normally, causing ‘null’ to be returned, but the return type is a potentially non-nullable type.’
I tried to find the miss, but can’t find what I missed.

  List sampleText0 = ['ちゅうしじょう'];
  List yoonList = ['ゃ','ゅ','ょ','ャ','ュ','ョ'];

  bool YoonTest(){
  for(int i = 0; i < 6; i++){
      if(sampleText0[0].contains(yoonList[i])){
        print('Loop$i , True');
        return(true);
      } else if(sampleText0[0].contains(yoonList[i])!){
        print('Loop$i');
        return(false);
      } else {
        return(false);
      }
    }
  }
  
  if(YoonTest()){
    print('True');
  } else {
    print('False');
  }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The analyzer is complaining that there is no explicit return value if the for loop is never entered or if the loop is interrupted before reaching one of the return statements. Given your code, that’s logically impossible, but the analyzer apparently doesn’t bother deducing that.

The code you’ve shown doesn’t make logical sense anyway:

  • The first two if conditions are equivalent.
  • There’s no point in looping if the loop body always returns (although I suspect that might be an artifact from your attempts to debug the problem).

You probably want something like:

var sampleText0 = ['ちゅうしじょう'];
var yoonList = ['ゃ', 'ゅ', 'ょ', 'ャ', 'ュ', 'ョ'];

bool YoonTest() {
  for (var character in yoonList) {
    if (sampleText0[0].contains(character)) {
      return true;
    }
  }
  return false;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading