Skip to content

Commit

Permalink
exclude null values
Browse files Browse the repository at this point in the history
  • Loading branch information
mrehan27 committed Nov 8, 2024
1 parent 5bdecac commit a661d96
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,7 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
val name = requireNotNull(params.getAsTypeOrNull<String>(Keys.Tracking.NAME)) {
"Event name is missing in params: $params"
}
val properties = params
.getAsTypeOrNull<Map<String, Any?>>(Keys.Tracking.PROPERTIES)
?.filterValues { it != null }
val properties = params.getAsTypeOrNull<Map<String, Any>>(Keys.Tracking.PROPERTIES)

if (properties.isNullOrEmpty()) {
CustomerIO.instance().track(name)
Expand Down Expand Up @@ -231,9 +229,7 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
val title = requireNotNull(params.getAsTypeOrNull<String>(Keys.Tracking.TITLE)) {
"Screen title is missing in params: $params"
}
val properties = params
.getAsTypeOrNull<Map<String, Any?>>(Keys.Tracking.PROPERTIES)
?.filterValues { it != null }
val properties = params.getAsTypeOrNull<Map<String, Any>>(Keys.Tracking.PROPERTIES)

if (properties.isNullOrEmpty()) {
CustomerIO.instance().screen(title)
Expand Down
7 changes: 5 additions & 2 deletions lib/customer_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'customer_io_config.dart';
import 'customer_io_enums.dart';
import 'customer_io_inapp.dart';
import 'customer_io_platform_interface.dart';
import 'extensions/map_extensions.dart';
import 'messaging_in_app/platform_interface.dart';
import 'messaging_push/platform_interface.dart';

Expand Down Expand Up @@ -108,7 +109,8 @@ class CustomerIO {
/// @param properties (Optional) params to be sent with event
void track(
{required String name, Map<String, dynamic> properties = const {}}) {
return _platform.track(name: name, properties: properties);
return _platform.track(
name: name, properties: properties.excludeNullValues());
}

/// Track a push metric
Expand All @@ -132,7 +134,8 @@ class CustomerIO {
/// @param attributes (Optional) params to be sent with event
void screen(
{required String title, Map<String, dynamic> properties = const {}}) {
return _platform.screen(title: title, properties: properties);
return _platform.screen(
title: title, properties: properties.excludeNullValues());
}

/// Use this function to send custom device attributes
Expand Down
7 changes: 7 additions & 0 deletions lib/extensions/map_extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// Extensions for [Map] class that provide additional functionality and convenience methods.
extension CustomerIOMapExtension on Map<String, dynamic> {
/// Returns a new map with entries that have non-null values, excluding null values.
Map<String, dynamic> excludeNullValues() {
return Map.fromEntries(entries.where((entry) => entry.value != null));
}
}

0 comments on commit a661d96

Please sign in to comment.