flutterdate-format

Flutter data returning error in debug console


I stored MillisecondsSinceEpoch in Firestore which is stored in a field as number. I want to retrieve the data in a Text widget so I convert it to DateTimeFormat.

In the UI everything is good. But in the debug console, I'm getting this error type 'Null' is not a subtype of type 'int'.

MY CODE

After streamBuild...

final deptTime = snapshot.data?.get("TimeToStart") //I also tried snapshot.data?.get("TimeToStart") ?? ""
final startTime = DateFormat("hh:mm a")
                     .format((DateTime
                             .fromMillisecondsSinceEpoch(deptTime)));
.....
Text(startTime)

What can I do to remove the error in the debug console?


Solution

  • Since deptTime is supposed to receive an Integer value, you shouldn't assign a string to it in case of the field TimeToStart is null

    final deptTime = snapshot.data?.get("TimeToStart")?? DateTime.now().millisecondsSinceEpoch;
    
    ...
    

    Note:

    the named constructor DateTime.fromMillisecondsSinceEpoch(intValue) requires an integer value as a positional parameter.

    make sure, snapshot.data?.get("TimeToStart") returns an integer if not, cast it to integer.