build static method
Builds request context information with automatic detection
screen - Current screen/page name (optional, app-specific)
environment - Environment (optional, e.g., "production", "development")
sessionId - Session identifier (optional, app-specific)
pushToken - Push notification token (optional, app-specific)
eventName - Event name (optional, extracted from eventData if not provided)
Implementation
static Future<Map<String, dynamic>> build({
String? screen,
String? environment,
String? sessionId,
String? pushToken,
String? eventName,
}) async {
final context = <String, dynamic>{
'os': _getOS(),
'osVersion': await _getOSVersion(),
'locale': _getLocale(),
'timezone': await getTimezone(),
'timestamp': DateTime.now().toIso8601String(),
'sdkVersion': EvntalyContext.sdkVersion,
};
// Auto-detect app info
try {
_packageInfo ??= await PackageInfo.fromPlatform();
context['appVersion'] = _packageInfo!.version;
context['appBuildNumber'] = _packageInfo!.buildNumber;
} catch (e) {
// Ignore if package info is not available
}
// Auto-detect device info
try {
if (Platform.isAndroid) {
final androidInfo = await _deviceInfo.androidInfo;
context['deviceModel'] = androidInfo.model;
context['deviceBrand'] = androidInfo.brand;
context['deviceId'] = androidInfo.id;
context['osVersion'] = 'Android ${androidInfo.version.release}';
// Carrier info for Android
try {
// Note: Carrier info requires additional permissions on Android
// This is a placeholder - you may need to use platform channels
context['carrier'] = androidInfo.manufacturer;
} catch (e) {
// Ignore carrier if not available
}
} else if (Platform.isIOS) {
final iosInfo = await _deviceInfo.iosInfo;
context['deviceModel'] = iosInfo.model;
context['deviceBrand'] = 'Apple';
context['deviceId'] = iosInfo.identifierForVendor ?? 'unknown';
context['osVersion'] = iosInfo.systemVersion;
// Carrier info for iOS requires additional setup
}
} catch (e) {
// Ignore if device info is not available
}
// Auto-detect network type
try {
final connectivityResult = await _connectivity.checkConnectivity();
context['networkType'] = _mapConnectivityResult(connectivityResult);
} catch (e) {
// Ignore if connectivity check fails
}
// Auto-detect battery info
try {
final batteryLevel = await _battery.batteryLevel;
context['batteryLevel'] = batteryLevel / 100.0; // Convert to 0.0-1.0
final batteryState = await _battery.batteryState;
context['isCharging'] = batteryState == BatteryState.charging ||
batteryState == BatteryState.full;
} catch (e) {
// Ignore if battery info is not available
}
// Auto-detect current screen
final detectedScreen = screen ?? getCurrentScreen();
if (detectedScreen != null) {
context['screen'] = detectedScreen;
}
// Auto-detect location and add it to requestContext
try {
final location = await EvntalyLocation.build();
if (location != null && location.isNotEmpty) {
context['location'] = location;
}
} catch (e) {
// Ignore if location detection fails
}
// Add optional app-specific fields if provided
if (environment != null) context['environment'] = environment;
if (sessionId != null) context['sessionId'] = sessionId;
if (pushToken != null) context['pushToken'] = pushToken;
if (eventName != null) context['eventName'] = eventName;
return context;
}