You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm not sure if there already is some underlying issue about this here, but I will just describe my problem:
As i continiued implementing BroadcastReceivers from #854, i wanted to get all the extras as a Map<String, dynamic> - I know there are already .getIntExtra() ..., which would be very type-safe, but I wanted to make use of Dart3 pattern matching ✨ So i made this:
@Keep
classTheLastUtils() {
companionobject {
@JvmStatic
fungetIntentExtras(intent:Intent): Map<String, Any?> {
val extras = intent.extras
val map = mutableMapOf<String, Any?>()
if (extras !=null) {
for (key in extras.keySet()) {
map[key] = extras.get(key)
}
}
return map
}
}
}
voidonReceive(android.Context context, android.Intent intent) {
// convert to Map<String, JObject>final map = android.TheLastUtils.getIntentExtras(intent)
.map((key, value) =>MapEntry(key.toDartString(), value));
// turn into a record for handi switch-case:switch((intent.getAction().toDartString(), map))
case (
android.BluetoothAdapter.ACTION_STATE_CHANGED,
{
android.BluetoothAdapter.EXTRA_STATE:int state,
android.BluetoothAdapter.EXTRA_PREVIOUS_STATE:int _,
// int's didn't match, so i later also tried:// ...EXTRA_STATE: JInteger state// but also didn't work
},
):// later, i catched them with var, and tried to:print('obj is:\n''int: ${stateisint}\n''bool: ${stateisbool}\n''jint: ${stateisJInteger}\n''then its: ${state.getClass()} ; ${state.runtimeType} ; ${state.$type}');
// it printed:// obj is:// int: false// bool: false // ofc// jint: false// then its: Instance of 'JClass' ; JObject ; Instance of 'JObjectType'// also, every time i tried to print(state), it printed the number corretly// so, i tried to dangerously:final jint =JInteger.fromRef(state.reference);
// and it worked// but also did this:final jbool =JBoolean.fromRef(state.reference);
// and this:print(jbool); // 13// only this finally crashed:print(jint.booleanValue() ?"true":"falsh");
// can't call boolean java.lang.Boolean.booleanValue() on instance of java.lang.Integer
}
So as of now, i don't see any way to tell what underlying class a JObject is :(
The text was updated successfully, but these errors were encountered:
i don't see any way to tell what underlying class a JObject is
You need to check the runtime type of a Java object by calling IsInstanceOf JNI function. It's easier to expose this functionality outside of Jni.env.IsInstanceOf(obj, clazz) and there is an issue tracking this: #1033.
It's not possible to do pattern matching on Java runtime type of an object.
Hello!
I'm not sure if there already is some underlying issue about this here, but I will just describe my problem:
As i continiued implementing
BroadcastReceiver
s from #854, i wanted to get all the extras as aMap<String, dynamic>
- I know there are already.getIntExtra() ...
, which would be very type-safe, but I wanted to make use of Dart3 pattern matching ✨ So i made this:So as of now, i don't see any way to tell what underlying class a
JObject
is :(The text was updated successfully, but these errors were encountered: