-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🆒 Parse json in a seperate isolate with compute
- Loading branch information
1 parent
c9c237c
commit f42e14c
Showing
4 changed files
with
20 additions
and
50 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,12 @@ | ||
coverage: | ||
precision: 2 | ||
round: down | ||
range: "70...100" | ||
|
||
status: | ||
project: no | ||
patch: no | ||
changes: no | ||
|
||
ignore: | ||
- "**/*.g.dart" |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,23 @@ | ||
import 'dart:convert'; | ||
import 'dart:isolate'; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter_workshop/base/base_api.dart'; | ||
import 'package:flutter_workshop/model/donation/donation.dart'; | ||
import 'package:flutter_workshop/model/donation/donation_api_response.dart'; | ||
import 'package:flutter_workshop/util/concurrency.dart'; | ||
import 'package:http/http.dart' as http; | ||
import 'package:meta/meta.dart'; | ||
|
||
DonationApiResponse parseDonations(Map<String, dynamic> json) => | ||
DonationApiResponse.fromJson(json); | ||
|
||
class DonationApi extends BaseApi { | ||
DonationApi({@required http.Client client}) : super(client: client); | ||
|
||
Future<List<Donation>> getDonations() async { | ||
final String url = '${baseUrl}listings/categories/33'; | ||
final http.Response response = await get(url); | ||
final Map<String, dynamic> json = jsonDecode(response.body); | ||
final dto = | ||
await computeInIsolate<DonationApiResponse>(parseDonations, json); | ||
final DonationApiResponse dto = await compute(parseDonations, json); | ||
return dto.donations; | ||
} | ||
} | ||
|
||
void parseDonations(SendPort sendPort) async { | ||
final port = ReceivePort(); | ||
sendPort.send(port.sendPort); | ||
|
||
await for (var message in port) { | ||
if (message is RespondableMessage) { | ||
final json = message.data; | ||
final result = DonationApiResponse.fromJson(json); | ||
message.sendPort.send(result); | ||
} | ||
|
||
port.close(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.