toDartString method

String toDartString(
  1. JStringPtr jstringPtr, {
  2. bool releaseOriginal = false,
})

Convenience method for converting a JStringPtr to dart string. if releaseOriginal is specified, jstring passed will be deleted using DeleteGlobalRef.

Implementation

String toDartString(JStringPtr jstringPtr, {bool releaseOriginal = false}) {
  if (jstringPtr == nullptr) {
    throw JNullError();
  }
  final chars = GetStringChars(jstringPtr, nullptr);
  if (chars == nullptr) {
    throw ArgumentError('Not a valid jstring pointer.');
  }
  final length = GetStringLength(jstringPtr);
  final result = chars.cast<Utf16>().toDartString(length: length);
  ReleaseStringChars(jstringPtr, chars);
  if (releaseOriginal) {
    DeleteGlobalRef(jstringPtr);
  }
  return result;
}