In a Flutter application, I’m trying to define my own view_cart event with custom parameters. So I decided to copy some of the code pieces from firebase_analytics library in order to send data successfully.
Here is the filterOutNulls function from firebase_analytics library:
Map<String, Object> filterOutNulls(Map<String, Object?> parameters) {
final Map<String, Object> filtered = <String, Object>{};
parameters.forEach((String key, Object? value) {
if (value != null) {
filtered[key] = value;
}
});
return filtered;
}
Here is the _marshalItems function from firebase_analytics library.
List<Map<String, dynamic>>? _marshalItems(List<AnalyticsEventItem>? items) {
if (items == null) return null;
return items.map((AnalyticsEventItem item) => item.asMap()).toList();
}
This is my logEvent function:
await FirebaseAnalytics.instance.logEvent(
name: 'view_cart',
parameters: filterOutNulls(<String, Object?>{
"user_id": userId,
"currency": convertSymbol(dvz),
"value": totalPrice,
"items": _marshalItems(list),
}),
);
// list variable is type of List<AnalyticsEventItem>
Finally, output of terminal:
[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: 'package:firebase_analytics/src/firebase_analytics.dart': Failed assertion: line 115 pos 9: 'value is String || value is num': 'string' OR 'number' must be set as the value of the parameter: items
#0 _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:51:61)
#1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart:40:5)
#2 FirebaseAnalytics.logEvent.<anonymous closure> (package:firebase_analytics/src/firebase_analytics.dart:115:9)
#3 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:617:13)
#4 FirebaseAnalytics.logEvent (package:firebase_analytics/src/firebase_analytics.dart:113:17)
What I don’t understand that the variables inside logEvent function is exact copy of logViewCart function. Even though they are same, logEvent function throws error but logViewCart does not.
Can anyone explain me where I did wrong?
2
Answers
I opened an issue in FlutterFire Github (check this out) and I saw that what I wanted to do is not supported by Google. It is not possible to assign a List of Objects to items parameter in custom logEvent.
So I have to stick with pre-defined logging functions that Firebsase Analytics allows.
Use This Method
you Can add List to the Value of the Map
Output