Created
January 13, 2023 15:08
-
-
Save fisherds/6d05365a42c5cf778c85f577618013ab to your computer and use it in GitHub Desktop.
I've been using this utility file to make getting fields un-crashable.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:cloud_firestore/cloud_firestore.dart'; | |
import 'package:flutter/material.dart'; | |
class FirestoreModelUtils { | |
// A few date time helpers that really don't belong here, but it's easy: | |
static DateTime fromTimestampToDateTime(Timestamp value) => value.toDate(); | |
static Timestamp fromDateTimeToTimestamp(DateTime value) => | |
Timestamp.fromDate(value); | |
static TimeOfDay fromDateTimeToTimeOfDay(DateTime value) { | |
DateTime epochDate = DateTime(1970, 1, 1, 0, 0); | |
Duration delta = value.difference(epochDate); | |
return TimeOfDay(hour: delta.inHours % 24, minute: delta.inMinutes % 60); | |
} | |
static String getStringField( | |
DocumentSnapshot documentSnapshot, String fieldName) => | |
containsField(documentSnapshot, fieldName) | |
? documentSnapshot.get(fieldName) | |
: ""; | |
static int getIntField(DocumentSnapshot documentSnapshot, String fieldName) => | |
containsField(documentSnapshot, fieldName) | |
? documentSnapshot.get(fieldName) | |
: 0; | |
static bool getBoolField( | |
DocumentSnapshot documentSnapshot, String fieldName) => | |
containsField(documentSnapshot, fieldName) | |
? documentSnapshot.get(fieldName) | |
: false; | |
static DateTime getDateTimeField( | |
DocumentSnapshot documentSnapshot, String fieldName) => | |
containsField(documentSnapshot, fieldName) | |
? documentSnapshot.get(fieldName)?.toDate() | |
: DateTime.now(); | |
static Timestamp getTimestampField( | |
DocumentSnapshot documentSnapshot, String fieldName) => | |
containsField(documentSnapshot, fieldName) | |
? documentSnapshot.get(fieldName) | |
: Timestamp.now(); | |
static bool containsField( | |
DocumentSnapshot documentSnapshot, String fieldName) => | |
documentSnapshot.exists && | |
(documentSnapshot.data() as Map<String, dynamic>) | |
.containsKey(fieldName) && | |
documentSnapshot.get(fieldName) != null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment