setAttributes method
Future<InappifySdkResult<List<Attribute> > >
setAttributes(
- InappifyAttributesRequest request
inherited
Stores or removes custom customer attributes.
Implementation
@override
Future<InappifySdkResult<List<Attribute>>> setAttributes(
InappifyAttributesRequest request,
) async {
final cancelled = _preCancelled<List<Attribute>>(
'setAttributes',
request.cancellation,
);
if (cancelled != null) return cancelled;
return _run<List<Attribute>>(
'setAttributes',
action: () async {
final values = InappifyWebPayloadCodec.attributeMap(request.attributes);
final updates = <String, Object?>{
for (final entry in values.entries)
if (entry.value != null) entry.key: entry.value,
};
final deletions = <String>[
for (final entry in values.entries)
if (entry.value == null) entry.key,
];
final idempotencyKey = request.idempotencyKey;
final requiresAtomicSync =
(updates.isNotEmpty && deletions.isNotEmpty) ||
(idempotencyKey?.isNotEmpty ?? false);
if (requiresAtomicSync) {
final options = <String, Object?>{
if (idempotencyKey?.isNotEmpty ?? false)
'idempotencyKey': idempotencyKey,
};
await _bridge.call(
'syncAttributes',
positionalArguments: <Object?>[
<Map<String, Object?>>[
for (final entry in values.entries)
<String, Object?>{'key': entry.key, 'value': entry.value},
],
if (options.isNotEmpty || request.cancellation != null) options,
],
cancellation: request.cancellation,
injectAbortSignal: request.cancellation != null,
);
} else if (updates.isNotEmpty) {
await _bridge.call(
'updateAttributes',
positionalArguments: <Object?>[
updates,
if (request.cancellation != null) const <String, Object?>{},
],
cancellation: request.cancellation,
injectAbortSignal: request.cancellation != null,
);
} else if (deletions.isNotEmpty) {
await _bridge.call(
'deleteAttributes',
positionalArguments: <Object?>[
deletions,
if (request.cancellation != null) const <String, Object?>{},
],
cancellation: request.cancellation,
injectAbortSignal: request.cancellation != null,
);
}
_readCustomerPropertyBestEffort();
return _currentAttributes;
},
mutationMayHaveCommitted: true,
cancellation: request.cancellation,
);
}