0

I was upgraded my flutter project version to the latest (2.5.3). then occurred some errors due to unsupported null-safety. I had fixed many but still, haven't any idea to fix following null-safety to error,

Code

itemBuilder: (context, suggestion) {
 return ListTile(
          subtitle: Text(
            suggestion.stateA == null || suggestion.stateB == null
                ? '${suggestion.city}, ${suggestion.country}'
                : '${suggestion.city}, ${suggestion.state}, ${suggestion.country}',
            style: Theme.of(context).textTheme.bodyText,
          ),
        );
 }
  

Errors

The property 'stateA' can't be unconditionally accessed because the receiver can be 'null'.
The property 'stateB' can't be unconditionally accessed because the receiver can be 'null'.
The property 'city' can't be unconditionally accessed because the receiver can be 'null'.
The property 'country' can't be unconditionally accessed because the receiver can be 'null'.

how add the check null-safety above code?

I already try with ! & ? Operator but it does not work for me

(i.e. suggestion!.state)

Tharindu Lakshan
  • 3,995
  • 6
  • 24
  • 44
  • try putting the logic of what to do if it's null outside Text Widget – quim Nov 12 '21 at 11:50
  • I tried that type too. but also still not fixed this issue. :( – Tharindu Lakshan Nov 12 '21 at 11:54
  • could you try doing something like: `Text(suggestion.city ?? whateverNotNull)`? – quim Nov 12 '21 at 11:59
  • if suggestion!.state not working then please add more info to your question – Diwyansh Nov 12 '21 at 12:12
  • There is very little context in your question. Why is your compiler assuming that `suggestion` can be null, is it a nullable variable? If you are sure it is not null, tell your compiler, by making it not nullable. – nvoigt Nov 12 '21 at 12:50

0 Answers0