-1
void f() {
  String string;
  setState(() {
    string = fooCondition ? 'foo' : 'bar';
  });
  
  string.toUpperCase(); // Error
}

If I remove the setState condition, the flow analysis work! I could put my setState below (blank setState(() {}) but that just reduce the readability of code. Is there any better way of doing it without making the string nullable and checking for nullability thereafter?

iDecode
  • 22,623
  • 19
  • 99
  • 186

1 Answers1

0

This is why you can also use late keyword in local scope.

late String string;

To answer your question:

void f() {
  late String string;
  setState(() {
    string = fooCondition ? 'foo' : 'bar';
  });

  string.toUpperCase(); // Works
}
iDecode
  • 22,623
  • 19
  • 99
  • 186