PrfDateTime constructor

PrfDateTime(
  1. String key, {
  2. DateTime? defaultValue,
})

Creates a new DateTime preference variable with the specified key.

The optional defaultValue is returned if the preference is not found or if an error occurs while reading.

Implementation

PrfDateTime(super.key, {super.defaultValue})
    : super(
        from: (base64) {
          if (base64 == null) return null;
          try {
            final bytes = base64Decode(base64);
            final millis =
                ByteData.sublistView(bytes).getInt64(0, Endian.big);
            return DateTime.fromMillisecondsSinceEpoch(millis);
          } catch (_) {
            return null;
          }
        },
        to: (dateTime) {
          final buffer = ByteData(8);
          buffer.setInt64(0, dateTime.millisecondsSinceEpoch, Endian.big);
          return base64Encode(buffer.buffer.asUint8List());
        },
        getter: (prefs, key) async => prefs.getString(key),
        setter: (prefs, key, value) async =>
            await prefs.setString(key, value),
      );