0

I have an API call that returns a null value and I want to handle it in the Text widget, if the value is null, return and empty string like this '' or and empty Text widget like this Text(''), if the text is not null, return the widget as it is.

 Text('FLIGHT ${data.locations.pickup.flightInfo.flightNumber}' ?? ''),

I just check it inside the Text widget like in the code above, is this not the correct approach? Thanks in advance for the help!

GrandMagus
  • 600
  • 3
  • 12
  • 37

2 Answers2

0

this is what you can do

Text(${data.locations.pickup.flightInfo.flightNumber} == null ? '' : 'FLIGHT ${data.locations.pickup.flightInfo.flightNumber}'),

though it seems long but it works or you can use null safety to do it

Brightcode
  • 660
  • 9
  • 27
0

You can easily use the null safety check like this :

Text(data.locations.pickup.flightInfo.flightNumber != null  ? 'FLIGHT ${data.locations.pickup.flightInfo.flightNumber}' : ""), 
Mohamad Alzabibi
  • 166
  • 1
  • 14