I am trying to convert a Firebase Timestamp to a DateTime to display in a text widget. This is all wrapped in a Streambuilder. The problem is that when I'm querying the data i don't know if there has been set a timestamp yet.
I have tried to try and catch multiple conversions but I always get an exception when I try to display the data.
startingString = DateFormat('kk:mm').format(snapshot.data['startingTime'].toDate());
this works fine if there is a timestamp in firebase but it fails if there is none.
Many thanks to everyone who can help me!!
It might just be a casting problem. Try this:
final timeStamp = snapshot.data['startingTime'] as TimeStamp;
var startingString = '--';
if (timeStamp == null) {
// null case
} else {
startingString = DateFormat('kk:mm').format(timeStamp.toDate());
}
try to add a field of type string and assign it a value of DateTime.now();
and then try to parse it using
var myTime = await DateTime.parse(snapshot.data['time']);
then format it.
Related
I'm having trouble extracting the values of the data received from the push notification:
Message data: {default: {"event":"text","id":"23Vlj0BwSA7xKtsf4IbFCHAIsNr"}}, where I tried a lot of .map methods etc. but I always get null values. Is there an easy approach to get the data from the message.data, so I can extract the value of the event key and the id value from the id key?
Can you try with the below code?
import 'dart:convert';
const rawJson =
'{"default": {"event":"text","id":"23Vlj0BwSA7xKtsf4IbFCHAIsNr"}}';
void parse() {
final value = json.decode(rawJson);
print(value['default']['event']);
print(value['default']['id']);
}
Output:
Thanks to #sanjay for the help, his solution wasn't working for me but it was close enough, only these 2 small changes worked:
NOTICE: Apart from #sanjay's answer I had to change json.decode to jsonDecode and I had to put a variable var instead of the constant value. I can understand the var and const, but I'm not sure about the jsonDecode method why I had to change it but it worked like this.
var value = jsonDecode(message.data['default']);
var event = value['event'];
var id = value['id'];
print(id);
Output:
23Vlj0BwSA7xKtsf4IbFCHAIsNr
Thanks for the help!
I am getting date and time from store. In data base its look like this
Need to know how can I show this as DD/MM/YY
I am trying to do like this
String timeString = snapshot.data[index]['lastupdate'].toString();
DateTime date = DateTime.parse(timeString);
print(DateFormat('yyyy-MM-dd').format(date));
Its showing error of Invalid date format
Try like this your lastupdate date is not convertime inDate thats why its showing error
DateTime date = DateTime.parse(snapshot.data[index]['lastupdate'].toDate().toString());
print(DateFormat('dd-MMM-yyy').format(date));
Use function to get date from Timestamp like this:
readDate(Timestamp dateTime) {
DateTime date = DateTime.parse(dateTime.toDate().toString());
// add DateFormat What you want. Look at the below comment example
//String formatedDate = DateFormat('dd-MMM-yyy').format(date);
String formatedDate = DateFormat.yMMMMd().format(date);
return formatedDate;
}
Use the function when you want it.
For example:
Text(readDOB(streamSnapshot.data!["dob"]))
For this you should install intl package. read
I'm using this package to render a timestamp in my flutter app. The timestamp is of type Timestamp in firestore. This is what I currently have:
//Helper method
static String ago(Timestamp timestamp) {
if (timestamp == null) return 'Unknown';
return timeago.format(timestamp.toDate()).toString();
}
and to display:
Text(Helpers.ago(order.timestamp))
It works fine, but it returns "timeago" in hours..how can I format to return minutes?
You could save it to a local variable then use
int.tryParse(value)
double.tryParse(value)
To get it in integer form. This can be mathematically converted to minutes. this is the long way
I found some question to this issue but none of them were for flutter. Basically I'm saving double value data in firestore number format but when the number is rounded for example 130.00 it save it as an integer. Now how can I make it double when retrieving the data. I've got simple model class which populate the data from map but I'm struggling to make it double there
factory Tool.fromMap(Map<String, dynamic> toolData) {
if (toolData == null) {
return null;
}
final double length = toolData['length']; //<-- how to make it double here
final String name = toolData['name'];
...
return Tool(
length: length,
name: name
...);
}
The known approaches doesn't seems to work here like
toolData['length'].toDouble()
UPDATE
Actually it works.. It just doesn't show as an option in android studio
I think parse method of double class could be solution for this.
double.parse(toolData['length'].toString());
I am writing test script for signup page and i need to put email address on each time. Can someone help me how to increment by value 1 in the email address as i execute the script for example, test#test.com and next time value should be test1#test.com. I an try with time stamp but not successfully work.
public class GetCurrentTimeStamp
{
public static void main( String[] args )
{
java.util.Date date= new java.util.Date();
System.out.println(new Timestamp(date.getTime()));
}
}
If you are trying to provide always unique email id, then you can use date with seconds as it keep changing also you can use
System.currentTimeMillis()
which gives number always unique. so you can append/concatenate it to email, i hope you know it.
You can use below code to get date
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date)); //2016/04/19 16:05:48
depends of simple date format provide 'yyyy/MM/dd HH:mm:ss' output will be displayed.
Thank You,
Murali
Use java.util.Date class instead of Timestamp and format it like so.
String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
String email= "test"+ timestamp + "#test.com";
Use below code:-
int num = 1; // Put this stament outside the for loop or put it as global variable.
Now use below code :-
num++;
String email= "test"+ num + "#test.com";
Hope it will help you :)