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');
}
CodePudding user response:
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
return
s (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;
}