Skip to content

Commit 1b4c7cf

Browse files
committed
try catch while getting ad in Android
1 parent 7cf028b commit 1b4c7cf

File tree

2 files changed

+37
-25
lines changed

2 files changed

+37
-25
lines changed

android/src/main/kotlin/dev/openflutter/flutter_advertising_id/FlutterAdvertisingIdPlugin.kt

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,36 +32,48 @@ class FlutterAdvertisingIdPlugin : FlutterPlugin, MethodCallHandler, CoroutineSc
3232
}
3333

3434
override fun onMethodCall(call: MethodCall, result: Result) {
35-
if (call.method == "getAdvertisingId") {
36-
applicationContext?.run {
37-
launch {
38-
val adInfo = withContext(Dispatchers.IO) {
39-
AdvertisingIdClient.getAdvertisingIdInfo(this@run.applicationContext)
40-
}
41-
withContext(Dispatchers.Main) {
42-
result.success(adInfo.id)
35+
when (call.method) {
36+
"getAdvertisingId" -> {
37+
applicationContext?.run {
38+
launch {
39+
val adInfo = withContext(Dispatchers.IO) {
40+
runCatching {
41+
AdvertisingIdClient.getAdvertisingIdInfo(this@run.applicationContext)
42+
}.getOrNull()
43+
}
44+
withContext(Dispatchers.Main) {
45+
result.success(adInfo?.id)
46+
}
4347
}
48+
} ?: run {
49+
result.success(null)
4450
}
45-
} ?: run {
46-
result.success(null)
4751
}
48-
} else if (call.method == "getAdvertisingId") {
49-
applicationContext?.run {
50-
launch {
51-
val adInfo = withContext(Dispatchers.IO) {
52-
AdvertisingIdClient.getAdvertisingIdInfo(this@run.applicationContext)
53-
}
54-
withContext(Dispatchers.Main) {
55-
result.success(adInfo.isLimitAdTrackingEnabled)
52+
53+
"limitAdTrackingEnabled" -> {
54+
applicationContext?.run {
55+
launch {
56+
val adInfo = withContext(Dispatchers.IO) {
57+
runCatching {
58+
AdvertisingIdClient.getAdvertisingIdInfo(this@run.applicationContext)
59+
}.getOrNull()
60+
}
61+
withContext(Dispatchers.Main) {
62+
result.success(adInfo?.isLimitAdTrackingEnabled ?: false)
63+
}
5664
}
65+
} ?: run {
66+
result.success(null)
5767
}
58-
} ?: run {
59-
result.success(null)
6068
}
61-
} else if (call.method == "authorizationStatus") {
62-
result.success(3)
63-
} else {
64-
result.notImplemented()
69+
70+
"authorizationStatus" -> {
71+
result.success(3)
72+
}
73+
74+
else -> {
75+
result.notImplemented()
76+
}
6577
}
6678
}
6779

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ packages:
77
path: ".."
88
relative: true
99
source: path
10-
version: "0.0.2"
10+
version: "0.0.3"
1111
async:
1212
dependency: transitive
1313
description:

0 commit comments

Comments
 (0)