Home > Software design >  Dart Unhandled Exception: FormatException: Invalid date format flutter
Dart Unhandled Exception: FormatException: Invalid date format flutter

Time:06-18

I have a DateTime in Realtime data base and i Have to stock it again in Firestore

I want to that string to be a TimeStamp but it returns an error :

Dart Unhandled Exception: FormatException: Invalid date format

timeHeart.onValue.listen((DatabaseEvent timeHeartevent) async{
                final HeartTime = timeHeartevent.snapshot.value ; 
             DateTime? newTime =  DateTime.parse(HeartTime as String ) ;  
             Timestamp myTimeStamp = Timestamp.fromDate(newTime) ;    
     db.collection("heartbeat").doc(HeartId.toString()).set(
               {
             'valeur': Heartvaleur, 
             'time': myTimeStamp , 
              'colorVal': "0xff96D7C6",
               });  
               });
               });
       });

CodePudding user response:

The problem is from this string 11:06:03-06-6-2022 is not in a valid date format. You can still parse it as below:

import 'package:intl/intl.dart'; // import at the top of the page.
// Needed for DateFormat.

...
final heartTime = timeHeartevent.snapshot.value; // '11:06:03-06-6-2022';
// heartTime spelt with small h.
DateTime? newTime = DateFormat("hh:mm:ss-dd-MM-yyyy").parse(heartTime);
print(newTime);

CodePudding user response:

Your date format should be list yyyy-MM-dd HH:mm:ss import package 'package:intl/intl.dart' DateTime.parse("2022-06-17")

  • Related