New static Java Strings are impossible to use in switch statements
- Dominant language
- Dart
- Stars
- 275
- Forks
- 144
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 47
Description
Hi!
Previously, i used `jni.BluetoothDevice.ACTION_SOMETHING_SOMETHING` static strings elegantly in `switch` statement, which made everything very elegant:
```dart
void onReceive(jni.Context context, jni.Intent intent) {
switch (intent.getAction().toDString()) {
case jni.BluetoothAdapter.ACTION_STATE_CHANGED:
final btState = intent.getIntExtra(
jni.BluetoothAdapter.EXTRA_STATE, -1);
switch (btState) {
case jni.BluetoothAdapter.STATE_ON:
_isEnabledCtrl.add(true);
break;
case jni.BluetoothAdapter.STATE_OFF:
_isEnabledCtrl.add(false);
break;
}
break;
case jni.BluetoothDevice.ACTION_BOND_STATE_CHANGED:
final extraDev = _getExtraDev(intent);
final bondState = intent.getIntExtra(
jni.BluetoothDevice.EXTRA_BOND_STATE, -1);
switch (bondState) {
case jni.BluetoothDevice.BOND_BONDED:
_pairedDevicesCtrl.add(
_pairedDevicesCtrl.value
..add(
_androidDevToDart(extraDev.dev),
),
);
break;
...
...
```
that's thanks to way they were simply statically generated:
```dart
static const ACTION_NAME_CHANGED =
r"""android.bluetooth.device.action.NAME_CHANGED""";
```
now, they are accesed through `_class.staticFieldId`, which makes it not-const for Dart and thus excluding it from switch
```dart
/// from: static public final java.lang.String ACTION_PAIRING_REQUEST
/// The returned object must be released after use, by calling the [release] method.
static jni.JString get ACTION_PAIRING_REQUEST =>
_id_ACTION_PAIRING_REQUEST.get(_class, const jni.JStringType());
static final _id_ACTION_UUID = _class.staticFieldId(
r'ACTION_UUID',
r'Ljava/lang/String;',
);
/// from: static public final java.lang.String ACTION_UUID
/// The returned object must be released after use, by calling the [release] method.
static jni.JString get ACTION_UUID =>
_id_ACTION_UUID.get(_class, const jni.JStringType());
...
```
intrestingly enough, `int` statics are generated directly:
```dart
/// from: static public final int ADDRESS_TYPE_PUBLIC
static const ADDRESS_TYPE_PUBLIC = 0;
/// from: static public final int ADDRESS_TYPE_RANDOM
static const ADDRESS_TYPE_RANDOM = 1;
...
```
...which does make it switch-able
---
I know there are probably some complicated memory/security/magic reasons behind this, but at the end of the day... please, do I really have to `if-elseif-elseif-elseif`, or `switch case _ when ... {} case _ when ...` like a caveman :worried: ?
Contributor guide
Assessment
This issue has not been assessed yet.