From 9afc682831729969cd5fd6f0aa68b4da1e899181 Mon Sep 17 00:00:00 2001 From: Fiqih Nur Ramadhan Date: Thu, 14 Mar 2024 11:20:20 +0700 Subject: [PATCH] Add Future return at some methods to prevent race condition in async code --- lib/mixpanel_flutter.dart | 45 +++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/lib/mixpanel_flutter.dart b/lib/mixpanel_flutter.dart index 4217865..003700e 100644 --- a/lib/mixpanel_flutter.dart +++ b/lib/mixpanel_flutter.dart @@ -1,8 +1,9 @@ import 'dart:async'; -import 'package:flutter/services.dart'; import 'dart:developer' as developer; import 'dart:io' show Platform; + import 'package:flutter/foundation.dart' show kIsWeb; +import 'package:flutter/services.dart'; import 'package:mixpanel_flutter/codec/mixpanel_message_codec.dart'; /// The primary class for integrating Mixpanel with your app. @@ -145,9 +146,9 @@ class Mixpanel { /// Mixpanel using the same disinct_id will be considered associated with the /// same visitor/customer for retention and funnel reporting, so be sure that the given /// value is globally unique for each individual user you intend to track. - void identify(String distinctId) { + Future identify(String distinctId) async { if (_MixpanelHelper.isValidString(distinctId)) { - _channel.invokeMethod( + await _channel.invokeMethod( 'identify', {'distinctId': distinctId}); } else { developer.log('`identify` failed: distinctId cannot be blank', @@ -189,9 +190,12 @@ class Mixpanel { /// /// * [eventName] The name of the event to send /// * [properties] An optional map containing the key value pairs of the properties to include in this event. - void track(String eventName, {Map? properties}) { + Future track( + String eventName, { + Map? properties, + }) async { if (_MixpanelHelper.isValidString(eventName)) { - _channel.invokeMethod('track', + await _channel.invokeMethod('track', {'eventName': eventName, 'properties': properties}); } else { developer.log('`track` failed: eventName cannot be blank', @@ -217,10 +221,13 @@ class Mixpanel { /// * [eventName] The name of the event to send /// * [properties] A Map containing the key value pairs of the properties to include in this event. /// * [groups] A Map containing the group key value pairs for this event. - void trackWithGroups(String eventName, Map properties, - Map groups) { + Future trackWithGroups( + String eventName, + Map properties, + Map groups, + ) async { if (_MixpanelHelper.isValidString(eventName)) { - _channel.invokeMethod('trackWithGroups', { + await _channel.invokeMethod('trackWithGroups', { 'eventName': eventName, 'properties': properties, 'groups': groups @@ -314,8 +321,8 @@ class Mixpanel { /// to remove a superProperty, call unregisterSuperProperty() or clearSuperProperties() /// /// * [properties] A Map containing super properties to register - void registerSuperProperties(Map properties) { - _channel.invokeMethod( + Future registerSuperProperties(Map properties) async { + await _channel.invokeMethod( 'registerSuperProperties', {'properties': properties}); } @@ -325,8 +332,10 @@ class Mixpanel { /// Calling registerSuperPropertiesOnce will never overwrite existing properties. /// /// * [properties] A Map containing the super properties to register. - void registerSuperPropertiesOnce(Map properties) { - _channel.invokeMethod('registerSuperPropertiesOnce', + Future registerSuperPropertiesOnce( + Map properties, + ) async { + await _channel.invokeMethod('registerSuperPropertiesOnce', {'properties': properties}); } @@ -337,9 +346,9 @@ class Mixpanel { /// To clear all superProperties, use clearSuperProperties() /// /// * [propertyName] name of the property to unregister - void unregisterSuperProperty(String propertyName) { + Future unregisterSuperProperty(String propertyName) async { if (_MixpanelHelper.isValidString(propertyName)) { - _channel.invokeMethod('unregisterSuperProperty', + await _channel.invokeMethod('unregisterSuperProperty', {'propertyName': propertyName}); } else { developer.log( @@ -364,8 +373,8 @@ class Mixpanel { /// superProperties registered before the clearSuperProperties method was called. /// /// To remove a single superProperty, use unregisterSuperProperty() - void clearSuperProperties() { - _channel.invokeMethod('clearSuperProperties'); + Future clearSuperProperties() async { + await _channel.invokeMethod('clearSuperProperties'); } /// Begin timing of an event. Calling timeEvent("Thing") will not send an event, but @@ -399,8 +408,8 @@ class Mixpanel { /// Clear super properties and generates a new random distinctId for this instance. /// Useful for clearing data when a user logs out. - void reset() { - _channel.invokeMethod('reset'); + Future reset() async { + await _channel.invokeMethod('reset'); } /// Returns the current distinct id of the user.