3

In code like the following, _decodeJson throws a FormatException.

final invalidJsonString = '{"key":"this is "invalid value" ."}';

try {
  await compute<String, dynamic>(
    _decodeJson,
    invalidJsonString,
  );
} on FormatException catch (_) {
  // error handling
}

static dynamic _decodeJson(String source) {
  return jsonDecode(source);
}

However, when I confirm this part, it seems that the exception thrown by the method executed by compute is converted to the Exception class. Is there a way to catch only FormatException using compute like the above code?

Flutter 1.22.5 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 7891006299 (6 weeks ago) • 2020-12-10 11:54:40 -0800
Engine • revision ae90085a84
Tools • Dart 2.10.4
kiri
  • 31
  • 2

1 Answers1

0

I think you can successfully catch FormatException by making it an Exception.

tamina
  • 1