skip to Main Content

I am new to dart, and I came across the following code. This apparently updates the document in the firebase. Can someone explain in detail, what it really does?


abstract class FirestoreDocumentUpdater {
  static Future<void> update(

      // ignore: use_function_type_syntax_for_parameters

      DocumentReference<Map<String, dynamic>> documentRef,
      Map<String, dynamic> map) {
    debugPrint('update function called');
    return documentRef.update(
      Map.fromEntries(
        map.entries.map(
          (e) {
            return MapEntry(
                e.key,
                switch (e.value) {
                  DateTime t => toJsonDateTime(t),
                  Duration d => toJsonDuration(d),
                  Enum t => t.name,
                  _ => e.value,
                });
          },
        ),
      ),
    );
  }
}
Timestamp? toJsonDateTime(DateTime? dateTime) {
  if (dateTime == null) return null;
  return Timestamp.fromDate(dateTime);
}


int toJsonDuration(Duration duration) {
  return duration.inMicroseconds;
}

2

Answers


  1. A MapEntry is simply the elements of a Map. Consider this map:

    Map a = {
       'key' : 'value',
       'foo' : 'bar'
    }
    

    This is a map with map entries MapEntry('key','value') and MapEntry('foo','bar');

    Now what that code does is simply change the values of a Map so that certain values are converted to another value. It turns this map:

    enum ExampleEnum {
      test
    }
    
    Map example = {
       'date' : DateTime.now(),
       'duration' : Duration(seconds: 6),
       'enum' : ExampleEnum.test,
       'other' : 'other'
    }
    

    into

    Map example = {
       'date' : someTimeStamp,
       'duration' : 6000000,
       'enum' : 'test',
       'other' : 'other'
    }
    
    Login or Signup to reply.
  2. The code is slightly more confusing than it needs to be due to a poor name choice. For this reason I am replacing the parameter map with inputMap.

    abstract class FirestoreDocumentUpdater {
    
      /// Merges the entries of [inputMap] with the entries of the
      /// referenced map.  
      /// Note: The entries of [inputMap] are transformed prior to 
      /// merging according to the rules: 
      /// * value is DateTime => toJsonDateTime(value)
      /// * value is Duration => toJsonDuration(value)
      /// * value is Enum => value.name
      static Future<void> update(
        DocumentReference<Map<String, dynamic>> documentRef,
        Map<String, dynamic> inputMap,
      ) {
        debugPrint('update function called');
        return documentRef.update(
          Map.fromEntries( // Creates a new map from map entries.
            inputMap.entries.map(  //  Iterates through `entries` and calls the 
              (e) {                //  method `map` transforming entries with 
                return MapEntry(   //  value of type `DateTime`, `Duration`, `Enum`.
                    e.key,
                    switch (e.value) {
                      DateTime t => toJsonDateTime(t),
                      Duration d => toJsonDuration(d),
                      Enum t => t.name,
                      _ => e.value,
                    });
              },
            ),
          ),
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search