Store a flutter Map with TimeStamp Values using SharedPreferences

Karim Boujnan
3 min readApr 24, 2023

--

Introducing MapDescriptor Package for Dart/Flutter

MapDescriptor is a Dart/Flutter package that helps to work with maps containing TimeStamp values. The package provides the ability to convert TimeStamp values in the map to ISO 8601 string format, and vice versa.

The MapDescriptor package is especially useful in working with Firebase Firestore data. Firestore stores TimeStampvalues as Firebase Timestamp objects, which can be a little cumbersome to work with. With MapDescriptor, you can convert these objects to TimeStamp values or ISO 8601 strings, making them easier to work with.

Features

  • Converts TimeStamp objects to ISO 8601 strings and vice versa.
  • Determines if a map contains any TimeStamp values.
  • Determines if a map contains any ISO 8601 string values.

Installation

Add the following dependency to your pubspec.yaml:

dependencies:
map_descriptor: ^0.1.3

Or just follow the package link on pub.dev

Usage

To use the MapDescriptor package, simply import it and create a new instance of the MapDescriptor class:

import 'package:map_descriptor/map_descriptor.dart';

MapDescriptor mapDescriptor = MapDescriptor();

Converting TimeStamp values to ISO 8601 strings

To convert TimeStamp values in a map to ISO 8601 strings, use the convertTimeStampToStr method:

Map<String, dynamic> myMap = {
'date1': TimeStamp(2124545,6565457),
'date2': {'date3': TimeStamp(1212154,121215545)}
};

Map<String, dynamic> result = mapDescriptor.convertTimeStampToStr(myMap);

print(result);
// output: {date1: 2023-04-24T16:47:36.975, date2: {date3: 2023-04-24T16:47:36.975}}

Converting ISO 8601 strings to TimeStamp values

To convert ISO 8601 string values in a map to TimeStamp objects, use the convertStrToTimeStamp method:

Map<String, dynamic> myMap = {
'date1': '2023-04-24T16:47:36.975',
'date2': {'date3': '2023-04-24T16:47:36.975'}
};

Map<String, dynamic> result = mapDescriptor.convertStrToTimeStamp(myMap);

print(result);
// output: {date1: Timestamp(seconds=1685065656, nanoseconds=975000000), date2: {date3: Timestamp(seconds=1685065656, nanoseconds=975000000)}}

Checking for TimeStamp or ISO 8601 string values

To determine if a map contains any TimeStamp or ISO 8601 string values, use the containsTimeStamp and containsISO8601Str methods, respectively:

Map<String, dynamic> myMap1 = {'date1': TimeStamp(4545412,2121454)};
Map<String, dynamic> myMap2 = {'date1': {'date3': TimeStamp(412115,121244)}, 'date2': {'date3': '2023-04-24T16:47:36.975'}};

bool containsDateTime1 = mapDescriptor.containsTimeStamp(myMap1); // true
bool containsDateTime2 = mapDescriptor.containsTimeStamp(myMap2); // false

bool containsISO8601Str1 = mapDescriptor.containsISO8601Str(myMap1); // false
bool containsISO8601Str2 = mapDescriptor.containsISO8601Str(myMap2); // true

Storing the map to the persistent storage

 Future setMap(
String key,
Map<String, dynamic> myMap,
) async {
await initializePrefs();
//delete the previous data to prevent error
await prefs!.setString(key, jsonEncode({}));
if (MapDescriptor().containsTimeStamp(myMap)) {
myMap = MapDescriptor().convertTimeStampToStr(myMap);
}
//now we can use jsonEncode for myMap
// if we kept myMap without converting TimeStamp values
// the code will throw an error that the method .toJson() is not allowed
// for TimeStamp values
return await prefs!.setString(key, jsonEncode(myMap));
}

If the myMap parameter contains a timestamp (as determined by the containsTimeStamp method of the MapDescriptor class), the convertTimeStampToStr method of the MapDescriptor class is called to convert the timestamp to a string.

Finally, the myMap parameter is JSON-encoded and saved to the persistent storage using the setString method of the prefs instance with the specified key.

Overall, this function saves a map of key-value pairs to a persistent storage using SharedPreferences. If the map contains a timestamp, it is converted to a string before being saved. The previous data associated with the specified key is deleted before saving the new data to prevent errors.

Conclusion

The MapDescriptor package for Dart/Flutter is a powerful tool that simplifies working with TimeStamp values in maps. With its ability to convert TimeStamp objects to ISO 8601 strings and vice versa, the package can save developers a lot of time and effort. It also provides methods to check if a map contains any TimeStampor ISO 8601 string values, making it a valuable asset when working with Firebase Firestore data. Overall, the MapDescriptor package is a great addition to the toolkit of any Dart/Flutter developer working with maps containing TimeStamp values.

--

--

Karim Boujnan
0 Followers

Flutter & IOT devices programmer | Creator of MapDescriptor package for Flutter developpers