dvInstallIdFrom function

String dvInstallIdFrom({
  1. required String? read(),
  2. required void write(
    1. String id
    ),
  3. String generate()?,
})

This install's id: the stored one, or a new random one stored for next time.

Random and install-scoped — not a user, not an advertising id — so it is gone when the application is reinstalled or its data cleared, as the specification says. A stored value that is not an id is replaced rather than trusted: whatever was written there would ride along on every report. Storage that cannot be read or written still yields an id, for this run only, rather than failing the installation.

Implementation

String dvInstallIdFrom({
  required String? Function() read,
  required void Function(String id) write,
  String Function()? generate,
}) {
  try {
    final String? stored = read()?.trim();
    if (stored != null && _installIdShape.hasMatch(stored)) return stored;
  } on Object {
    // Unreadable: a new id below.
  }
  final String id = (generate ?? dvAnalyticsRandomId)();
  try {
    write(id);
  } on Object {
    // Unwritable: this run still has an id.
  }
  return id;
}