logEvent method
Logs an event using the analytics provider from the surrounding provider scope.
This method also automatically adds additional information with the event, such as the current route (based on the surrounding modal route).
It also supports boolean values for parameters, unlike Firebase which
only supports strings and numbers.
Implementation
Future<void> logEvent(
String name, {
Map<String, Object>? parameters,
}) {
final scope =
getElementForInheritedWidgetOfExactType<UncontrolledProviderScope>()
?.widget as UncontrolledProviderScope?;
if (scope == null) {
if (kDebugMode) {
print('No provider scope found. "$name" event was not logged.');
}
return Future.value();
}
final container = scope.container;
final analytics = container.read(analyticsProvider);
final route = ModalRoute.of(this);
final screenName = route?.settings.name;
return analytics.logEvent(
name: name,
parameters: {
if (screenName != null) 'screen_name': screenName,
if (parameters != null)
for (final paramEntry in parameters.entries)
if (paramEntry.value is bool)
paramEntry.key: (paramEntry.value as bool) ? 'true' : 'false'
else if (paramEntry.value is DateTime)
paramEntry.key:
(paramEntry.value as DateTime).toUtc().toIso8601String()
else
paramEntry.key: paramEntry.value,
},
);
}