Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ private fun Map<String, Any>.convert(): ConfidenceValue {
"double" -> return ConfidenceValue.Double(this["value"] as Double)
"bool" -> return ConfidenceValue.Boolean(this["value"] as Boolean)
"int" -> return ConfidenceValue.Integer(this["value"] as Int)
// Dart could not map this type (a DateTime, a null, a custom object) and
// has already sent `value.toString()`. Keep that string rather than
// throwing: `track` is fire-and-forget from Dart, so an exception here
// would surface only as a logged handler error while the event is lost.
"unknown" -> return ConfidenceValue.String(this["value"]?.toString() ?: "")
"list" -> {
val list = (this["value"] as List<Map<String, Any>>).map { it.convert() }
return ConfidenceValue.List(list)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,17 @@ func convertValue(_ type: String, _ value: Any) -> ConfidenceValue {
})
case "string":
return ConfidenceValue.init(string: value as! String)
case "unknown":
// Dart could not map this type (a DateTime, a null, a custom object)
// and has already sent `value.toString()`. Keep that string: coercing
// it to a number would publish a wrong value with nothing to show the
// caller their data was discarded.
return ConfidenceValue.init(string: value as? String ?? String(describing: value))
default:
return ConfidenceValue.init(integer: 0)
// An unrecognised type marker means the Dart and native sides have
// drifted. Preserve the value as a string and make the mismatch
// visible rather than silently publishing a number.
NSLog("%@", "Confidence SDK: unsupported value type '\(type)', publishing it as a string")
return ConfidenceValue.init(string: value as? String ?? String(describing: value))
}
}
8 changes: 8 additions & 0 deletions lib/confidence_flutter_sdk_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ class MethodChannelConfidenceFlutterSdk extends ConfidenceFlutterSdkPlatform {
return value!;
}

/// Wraps [value] in the `{'type': ..., 'value': ...}` envelope the native
/// plugins decode.
///
/// Anything Confidence has no type for — a `DateTime`, a `null`, a custom
/// object — is sent as `type: 'unknown'` carrying `value.toString()`. Both
/// native sides publish that as a string. They must not coerce it to a
/// number or drop it: the caller's data would be silently wrong with
/// nothing to indicate it.
Map<String, dynamic> toTypedValue(dynamic value) {
if (value is int) {
return {'type': 'int', 'value': value};
Expand Down
43 changes: 43 additions & 0 deletions test/confidence_flutter_sdk_method_channel_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,47 @@ void main() {
expect(methodCalls, hasLength(1));
expect(methodCalls.single.method, 'track');
});

// Values Confidence has no type for must reach native as a stringified
// 'unknown', never as a number and never dropped. The native halves of this
// contract (iOS convertValue, Android convert) cannot be exercised from
// Dart, so what is pinned here is the wire format they rely on.
group('unsupported tracking values keep their value', () {
Map<String, dynamic> trackedData() {
final args = methodCalls.single.arguments as Map<Object?, Object?>;
return (args['data'] as Map<Object?, Object?>)
.map((k, v) => MapEntry(k as String, v));
}

test('a DateTime is stringified, not coerced to a number', () {
final now = DateTime.utc(2026, 9, 7, 12, 34, 56);
platform.track('my_event', {'ts': now});

final ts = trackedData()['ts'] as Map<Object?, Object?>;
expect(ts['type'], 'unknown');
expect(ts['value'], now.toString());
expect(ts['value'], isNot(0));
});

test('a null keeps a value and the key is not dropped', () {
platform.track('my_event', {'maybe': null});

final data = trackedData();
expect(data.containsKey('maybe'), isTrue,
reason: 'the key must survive so the caller can see what was sent');
final maybe = data['maybe'] as Map<Object?, Object?>;
expect(maybe['type'], 'unknown');
expect(maybe['value'], isNot(0));
});

test('supported types are unaffected', () {
platform.track('my_event', {'n': 7, 's': 'x', 'b': true, 'd': 1.5});

final data = trackedData();
expect((data['n'] as Map<Object?, Object?>)['type'], 'int');
expect((data['s'] as Map<Object?, Object?>)['type'], 'string');
expect((data['b'] as Map<Object?, Object?>)['type'], 'bool');
expect((data['d'] as Map<Object?, Object?>)['type'], 'double');
});
});
}