Showing
1 changed file
with
14 additions
and
45 deletions
| @@ -14,63 +14,32 @@ | @@ -14,63 +14,32 @@ | ||
| 14 | * limitations under the License. | 14 | * limitations under the License. |
| 15 | */ | 15 | */ |
| 16 | 16 | ||
| 17 | -import 'dart:async'; | ||
| 18 | import 'dart:io'; | 17 | import 'dart:io'; |
| 19 | import 'dart:isolate'; | 18 | import 'dart:isolate'; |
| 20 | -import 'dart:typed_data'; | ||
| 21 | 19 | ||
| 22 | import 'package:pdf/widgets.dart'; | 20 | import 'package:pdf/widgets.dart'; |
| 21 | +import 'package:test/test.dart'; | ||
| 23 | 22 | ||
| 24 | import 'utils.dart'; | 23 | import 'utils.dart'; |
| 25 | 24 | ||
| 26 | -class Message { | ||
| 27 | - Message(this.image, this.sendPort); | ||
| 28 | - | ||
| 29 | - final Uint8List image; | ||
| 30 | - final SendPort sendPort; | ||
| 31 | -} | ||
| 32 | - | ||
| 33 | -void compute(Message message) { | ||
| 34 | - final pdf = Document(); | ||
| 35 | - | ||
| 36 | - final image = MemoryImage( | ||
| 37 | - message.image, | ||
| 38 | - ); | ||
| 39 | - | ||
| 40 | - pdf.addPage(Page(build: (Context context) => Center(child: Image(image)))); | ||
| 41 | - | ||
| 42 | - message.sendPort.send(pdf.save()); | ||
| 43 | -} | ||
| 44 | - | ||
| 45 | void main() { | 25 | void main() { |
| 46 | - noTest('Pdf Isolate', () async { | ||
| 47 | - final completer = Completer<void>(); | ||
| 48 | - final receivePort = ReceivePort(); | ||
| 49 | - | ||
| 50 | - receivePort.listen((dynamic data) async { | ||
| 51 | - if (data is Uint8List) { | ||
| 52 | - print('Received a ${data.length} bytes PDF'); | ||
| 53 | - final file = File('isolate.pdf'); | ||
| 54 | - await file.writeAsBytes(data); | ||
| 55 | - print('File saved'); | ||
| 56 | - completer.complete(); | ||
| 57 | - } | ||
| 58 | - }); | ||
| 59 | - | 26 | + test('Pdf Isolate', () async { |
| 60 | print('Download image'); | 27 | print('Download image'); |
| 61 | final imageBytes = await download('https://www.nfet.net/nfet.jpg'); | 28 | final imageBytes = await download('https://www.nfet.net/nfet.jpg'); |
| 62 | 29 | ||
| 63 | print('Generate PDF'); | 30 | print('Generate PDF'); |
| 64 | - await Isolate.spawn<Message>( | ||
| 65 | - compute, | ||
| 66 | - Message(imageBytes, receivePort.sendPort), | ||
| 67 | - ); | 31 | + // ignore: sdk_version_since |
| 32 | + final data = await Isolate.run(() async { | ||
| 33 | + final pdf = Document(); | ||
| 34 | + final image = MemoryImage(imageBytes); | ||
| 35 | + pdf.addPage( | ||
| 36 | + Page(build: (Context context) => Center(child: Image(image)))); | ||
| 37 | + return await pdf.save(); | ||
| 38 | + }); | ||
| 68 | 39 | ||
| 69 | - print('Wait PDF to be generated'); | ||
| 70 | - await completer.future; | ||
| 71 | - receivePort.close(); | ||
| 72 | - print('Done'); | 40 | + print('Generated a ${data.length} bytes PDF'); |
| 41 | + final file = File('isolate.pdf'); | ||
| 42 | + await file.writeAsBytes(data); | ||
| 43 | + print('File saved'); | ||
| 73 | }); | 44 | }); |
| 74 | } | 45 | } |
| 75 | - | ||
| 76 | -void noTest(String s, Future<void> Function() param1) {} |
-
Please register or login to post a comment