Forked from gladimdim/process_exceptions_in_isolates.dart
Created
December 13, 2021 14:19
-
-
Save rutvik110/b96c76904f824608d34214ada5beba82 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class JsonParserIsolate { | |
final String input; | |
JsonParserIsolate(this.input); | |
Future parseJson({Function(String)? onError}) async { | |
final completer = Completer(); | |
var port = ReceivePort(); | |
var errorPort = ReceivePort(); | |
await Isolate.spawn(_parse, port.sendPort, onError: errorPort.sendPort); | |
errorPort.listen((message) { | |
if (onError != null) { | |
// first is Error Message | |
// second is stacktrace which is not needed | |
List errors = message as List; | |
errorPort.close(); | |
onError(errors.first); | |
} | |
}); | |
port.listen((message) { | |
port.close(); | |
completer.complete(message); | |
}); | |
return completer.future; | |
} | |
Future<void> _parse(SendPort p) async { | |
final json = jsonDecode(input); | |
Isolate.exit(p, json); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment