Even below code already check t.s!=null
, dart still throws Error:
t.dart:7:26: Error: Property 'length' cannot be accessed on 'String?' because it is potentially null. Try accessing using ?. instead. if (t.s != null && t.s.length > 5) {}
class Test {
String? s;
}
void main() {
Test t = Test();
if (t.s != null && t.s.length > 5) {}
}
Add extra var
would solve it as below:
void main() {
Test t = Test();
var s = t.s;
if (s != null && s.length > 5) {}
}
Why dart throws error even t.s!=null
already checked?
Is there a way to do it without adding extra var
?
Additionally, in Typescript, it won't throw error:
function main(t:{s?:string}){
if(t.s!==undefined && t.s.length > 5){
}
}