|
1 | 1 | import 'dart:async';
|
2 |
| -import 'dart:io'; |
3 | 2 |
|
4 | 3 | import 'package:flutter/material.dart';
|
5 | 4 | import 'package:flutter_isolate/flutter_isolate.dart';
|
6 | 5 | import 'package:path_provider/path_provider.dart';
|
| 6 | +import 'package:flutter_downloader/flutter_downloader.dart'; |
7 | 7 |
|
8 | 8 | @pragma('vm:entry-point')
|
9 | 9 | void isolate2(String arg) {
|
10 |
| - HttpClient? httpClient; |
11 |
| - HttpClientRequest? request; |
12 |
| - var url = Uri.parse('https://raw.githubusercontent.com/rmawatson/flutter_isolate/master/README.md'); |
13 |
| - |
14 | 10 | getTemporaryDirectory().then((dir) async {
|
15 |
| - print("isolate2 downloading $url"); |
16 |
| - httpClient?.close(); |
17 |
| - request?.close(); |
18 |
| - httpClient = HttpClient(); |
19 |
| - request = await httpClient!.getUrl(url); |
20 |
| - print("downloaded the file."); |
21 |
| - }).catchError((e) { |
22 |
| - print(e); |
| 11 | + print("isolate2 temporary directory: $dir"); |
| 12 | + |
| 13 | + await FlutterDownloader.initialize(debug: true); |
| 14 | + FlutterDownloader.registerCallback(AppWidget.downloaderCallback); |
| 15 | + |
| 16 | + var url = 'https://raw.githubusercontent.com/rmawatson/flutter_isolate/master/README.md'; |
| 17 | + |
| 18 | + // ignore: unused_local_variable |
| 19 | + final taskId = await FlutterDownloader.enqueue(url: url, savedDir: dir.path); |
23 | 20 | });
|
24 | 21 | Timer.periodic(Duration(seconds: 1), (timer) => print("Timer Running From Isolate 2"));
|
25 | 22 | }
|
@@ -87,59 +84,61 @@ class AppWidget extends StatelessWidget {
|
87 | 84 | @override
|
88 | 85 | Widget build(BuildContext context) {
|
89 | 86 | return Center(
|
90 |
| - child: Column(crossAxisAlignment: CrossAxisAlignment.center, children: [ |
91 |
| - Padding( |
92 |
| - padding: const EdgeInsets.only(top: 30), |
93 |
| - child: ElevatedButton( |
94 |
| - child: Text('Spawn isolates'), |
95 |
| - onPressed: _run, |
96 |
| - ), |
97 |
| - ), |
98 |
| - Padding( |
99 |
| - padding: const EdgeInsets.only(top: 30), |
100 |
| - child: ElevatedButton( |
101 |
| - child: Text('Check running isolates'), |
102 |
| - onPressed: () async { |
103 |
| - final isolates = await FlutterIsolate.runningIsolates; |
104 |
| - await showDialog( |
105 |
| - builder: (ctx) { |
106 |
| - return Center( |
107 |
| - child: Container( |
108 |
| - color: Colors.white, |
109 |
| - padding: EdgeInsets.all(5), |
110 |
| - child: Column( |
111 |
| - children: isolates.map((i) => Text(i)).cast<Widget>().toList() + |
112 |
| - [ |
113 |
| - ElevatedButton( |
114 |
| - child: Text("Close"), |
115 |
| - onPressed: () { |
116 |
| - Navigator.of(ctx).pop(); |
117 |
| - }) |
118 |
| - ]))); |
119 |
| - }, |
120 |
| - context: context); |
121 |
| - }, |
122 |
| - ), |
123 |
| - ), |
124 |
| - Padding( |
125 |
| - padding: const EdgeInsets.only(top: 30), |
126 |
| - child: ElevatedButton( |
127 |
| - child: Text('Kill all running isolates'), |
128 |
| - onPressed: () async { |
129 |
| - await FlutterIsolate.killAll(); |
130 |
| - }, |
131 |
| - ), |
132 |
| - ), |
133 |
| - Padding( |
134 |
| - padding: const EdgeInsets.only(top: 30), |
135 |
| - child: ElevatedButton( |
136 |
| - child: Text('Run in compute function'), |
137 |
| - onPressed: () async { |
138 |
| - await flutterCompute(computeFunction, "foo"); |
139 |
| - }, |
140 |
| - ), |
141 |
| - ), |
142 |
| - ]), |
| 87 | + child: Column( |
| 88 | + crossAxisAlignment: CrossAxisAlignment.center, |
| 89 | + children: [ |
| 90 | + Padding( |
| 91 | + padding: const EdgeInsets.only(top: 30), |
| 92 | + child: ElevatedButton( |
| 93 | + child: Text('Spawn isolates'), |
| 94 | + onPressed: _run, |
| 95 | + ), |
| 96 | + ), |
| 97 | + Padding( |
| 98 | + padding: const EdgeInsets.only(top: 30), |
| 99 | + child: ElevatedButton( |
| 100 | + child: Text('Check running isolates'), |
| 101 | + onPressed: () async { |
| 102 | + final isolates = await FlutterIsolate.runningIsolates; |
| 103 | + await showDialog( |
| 104 | + builder: (ctx) { |
| 105 | + return Center( |
| 106 | + child: Container( |
| 107 | + color: Colors.white, |
| 108 | + padding: EdgeInsets.all(5), |
| 109 | + child: Column( |
| 110 | + children: isolates.map((i) => Text(i)).cast<Widget>().toList() + |
| 111 | + [ |
| 112 | + ElevatedButton( |
| 113 | + child: Text("Close"), |
| 114 | + onPressed: () { |
| 115 | + Navigator.of(ctx).pop(); |
| 116 | + }) |
| 117 | + ]))); |
| 118 | + }, |
| 119 | + context: context); |
| 120 | + }, |
| 121 | + ), |
| 122 | + ), |
| 123 | + Padding( |
| 124 | + padding: const EdgeInsets.only(top: 30), |
| 125 | + child: ElevatedButton( |
| 126 | + child: Text('Kill all running isolates'), |
| 127 | + onPressed: () async { |
| 128 | + await FlutterIsolate.killAll(); |
| 129 | + }, |
| 130 | + ), |
| 131 | + ), |
| 132 | + Padding( |
| 133 | + padding: const EdgeInsets.only(top: 30), |
| 134 | + child: ElevatedButton( |
| 135 | + child: Text('Run in compute function'), |
| 136 | + onPressed: () async { |
| 137 | + await flutterCompute(computeFunction, "foo"); |
| 138 | + }, |
| 139 | + ), |
| 140 | + ), |
| 141 | + ]), |
143 | 142 | );
|
144 | 143 | }
|
145 | 144 | }
|
0 commit comments