Function to dynamically remove DocumentReference & DateTime from json before passing to custom functions to prevent error

Custom data types are great, and the new integration with cloud functions is definitely helpful when it comes to speeding up the rate of development. That being said, one issue is that the FlutterFlow default "to json" method fails when DocumentReference and DateTimes are present.

Below is a simple function that takes the built in to json method provided by FlutterFlow, and dynamically eliminates the properties which will cause error. You can also add logic to handle properties which cause error, such as using the doc ID and then grabbing it on the backend, etc. Hope this helps!

dynamic jsonEncodeExcludingErrors(dynamic dataToJson) {

/// MODIFY CODE ONLY BELOW THIS LINE

Map<String, dynamic> sanitizedMap = {};

dataToJson.forEach((key, value) {

try {

// Try encoding each value. If it succeeds, include it in the sanitized map.

jsonEncode(value);

sanitizedMap[key] = value;

} catch (e) {

if (e is JsonUnsupportedObjectError) {

// If a JsonUnsupportedObjectError is caught, exclude the value from the sanitized map.

print('Excluding non-encodable property: $key');

} else {

// Re-throw any other exceptions.

rethrow;

}

}

});

return sanitizedMap;

/// MODIFY CODE ONLY ABOVE THIS LINE

}

1