Setting incoming Json data as Unix Timestamp to nullable

Issue

I have to convert the data I get through the API and that’s why I couldn’t set it to nullable.

var releaseDateTime =
        DateTime.fromMillisecondsSinceEpoch(_data.releaseDate! * 1000);
String releaseDate = DateFormat('yyyy-MM-dd').format(releaseDateTime);

Text(releaseDate),

But the incoming data may come as null or "0". So,

How can I print "N/A" in case of null or "0" depending on the condition.

Solution

You should check value before pass data in DateTime.fromMillisecondsSinceEpoch()

check below sample :

String releaseDate = "N/A"

if(_data.releaseDate != null && _data.releaseDate != "0"){
var releaseDateTime =
        DateTime.fromMillisecondsSinceEpoch(_data.releaseDate! * 1000);
 releaseDate = DateFormat('yyyy-MM-dd').format(releaseDateTime);
}

Text(releaseDate),

Answered By – mahmoud_eslami

Answer Checked By – Clifford M. (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.