petsHero-AI/lib/core/referrer/referrer_service.dart
2026-03-09 20:55:41 +08:00

39 lines
1.2 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:play_install_referrer/play_install_referrer.dart';
/// 安装来源 referrer 服务(用于 ch/crest 渠道参数)
class ReferrerService {
ReferrerService._();
static String? _cachedReferrer;
static final Completer<String?> _completer = Completer<String?>();
/// 获取 referrerAndroid 使用 Google Play Install ReferreriOS 返回空
static Future<String?> getReferrer() async {
if (_cachedReferrer != null) return _cachedReferrer;
if (_completer.isCompleted) return _completer.future;
if (defaultTargetPlatform != TargetPlatform.android) {
_cachedReferrer = '';
if (!_completer.isCompleted) _completer.complete('');
return '';
}
try {
final details = await PlayInstallReferrer.installReferrer;
_cachedReferrer = details.installReferrer ?? '';
} catch (_) {
_cachedReferrer = '';
}
if (!_completer.isCompleted) _completer.complete(_cachedReferrer);
return _cachedReferrer;
}
/// 初始化并缓存 referrer建议在 app 启动时调用)
static Future<void> init() async {
await getReferrer();
}
}