diff --git a/MAINTENANCE.md b/MAINTENANCE.md index 7df6b8d99..17292fbf4 100644 --- a/MAINTENANCE.md +++ b/MAINTENANCE.md @@ -10,7 +10,7 @@ match any new language/SDK features, etc.). | chrome-os-best-practices | | | | experimental | | | | flutter_maps_firestore | | | -| isolate_example | redbrogdon | 9/12/19 | +| isolate_example | johnpryan | 11/21/19 | | jsonexample | | | | material_studies/shrine | | | | place_tracker | | | diff --git a/isolate_example/lib/page_three.dart b/isolate_example/lib/data_transfer_page.dart similarity index 91% rename from isolate_example/lib/page_three.dart rename to isolate_example/lib/data_transfer_page.dart index b47b1ca59..f72cb434e 100644 --- a/isolate_example/lib/page_three.dart +++ b/isolate_example/lib/data_transfer_page.dart @@ -142,7 +142,9 @@ class DataTransferIsolateController extends ChangeNotifier { } Future generateRandomNumbers(bool transferableTyped) async { - if (running) return; + if (running) { + return; + } if (transferableTyped) { runningTest = 2; @@ -150,7 +152,7 @@ class DataTransferIsolateController extends ChangeNotifier { runningTest = 1; } - Random rng = Random(); + var random = Random(); currentProgress.clear(); @@ -162,7 +164,7 @@ class DataTransferIsolateController extends ChangeNotifier { randNums.clear(); for (int j = 0; j < 1000000; j++) { - randNums.add(rng.nextInt(100)); + randNums.add(random.nextInt(100)); } if (transferableTyped) { @@ -221,28 +223,28 @@ class RunningList extends StatelessWidget { } } -Future _secondIsolateEntryPoint(SendPort callerSP) async { - ReceivePort newIceRP = ReceivePort(); - callerSP.send(newIceRP.sendPort); +Future _secondIsolateEntryPoint(SendPort sendPort) async { + var receivePort = ReceivePort(); + sendPort.send(receivePort.sendPort); int length = 1; - newIceRP.listen( + receivePort.listen( (dynamic message) async { if (message is String && message == 'start') { - await generateAndSum(callerSP, createNums(), length); + await generateAndSum(sendPort, createNums(), length); - callerSP.send('done'); + sendPort.send('done'); } else if (message is TransferableTypedData) { await generateAndSum( - callerSP, message.materialize().asInt32List(), length); + sendPort, message.materialize().asInt32List(), length); length++; } else if (message is List) { - await generateAndSum(callerSP, message, length); + await generateAndSum(sendPort, message, length); length++; } if (length == 101) { - callerSP.send('done'); + sendPort.send('done'); length = 1; } }, @@ -250,9 +252,9 @@ Future _secondIsolateEntryPoint(SendPort callerSP) async { } Iterable createNums() sync* { - final rng = Random(); + var random = Random(); for (int i = 0; i < 100000000; i++) { - yield rng.nextInt(100); + yield random.nextInt(100); } } diff --git a/isolate_example/lib/page_two.dart b/isolate_example/lib/infinite_process_page.dart similarity index 96% rename from isolate_example/lib/page_two.dart rename to isolate_example/lib/infinite_process_page.dart index d5a91ed0e..831ff1ef9 100644 --- a/isolate_example/lib/page_two.dart +++ b/isolate_example/lib/infinite_process_page.dart @@ -102,7 +102,7 @@ class InfiniteProcessPage extends StatelessWidget { class InfiniteProcessIsolateController extends ChangeNotifier { Isolate newIsolate; - ReceivePort mIceRP; + ReceivePort receivePort; SendPort newIceSP; Capability capability; @@ -120,12 +120,13 @@ class InfiniteProcessIsolateController extends ChangeNotifier { List get currentResults => _currentResults; Future createIsolate() async { - mIceRP = ReceivePort(); - newIsolate = await Isolate.spawn(_secondIsolateEntryPoint, mIceRP.sendPort); + receivePort = ReceivePort(); + newIsolate = + await Isolate.spawn(_secondIsolateEntryPoint, receivePort.sendPort); } void listen() { - mIceRP.listen((dynamic message) { + receivePort.listen((dynamic message) { if (message is SendPort) { newIceSP = message; newIceSP.send(_currentMultiplier); @@ -224,7 +225,9 @@ Future _secondIsolateEntryPoint(SendPort callerSP) async { callerSP.send(newIceRP.sendPort); newIceRP.listen((dynamic message) { - if (message is int) multiplyValue = message; + if (message is int) { + multiplyValue = message; + } }); // This runs until the isolate is terminated. diff --git a/isolate_example/lib/main.dart b/isolate_example/lib/main.dart index dcf6ca594..d3ba4695e 100644 --- a/isolate_example/lib/main.dart +++ b/isolate_example/lib/main.dart @@ -14,19 +14,19 @@ import 'package:flutter/material.dart'; -import 'page_one.dart'; -import 'page_three.dart'; -import 'page_two.dart'; +import 'data_transfer_page.dart'; +import 'infinite_process_page.dart'; +import 'performance_page.dart'; void main() { runApp( MaterialApp( - home: StartApp(), + home: HomePage(), ), ); } -class StartApp extends StatelessWidget { +class HomePage extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( diff --git a/isolate_example/lib/page_one.dart b/isolate_example/lib/performance_page.dart similarity index 63% rename from isolate_example/lib/page_one.dart rename to isolate_example/lib/performance_page.dart index ec199e20d..459770db8 100644 --- a/isolate_example/lib/page_one.dart +++ b/isolate_example/lib/performance_page.dart @@ -17,15 +17,15 @@ import 'package:flutter/material.dart'; // Computes the nth number in the Fibonacci sequence. int fib(int n) { - int number1 = n - 1; - int number2 = n - 2; + var a = n - 1; + var b = n - 2; if (n == 1) { return 0; } else if (n == 0) { return 1; } else { - return (fib(number1) + fib(number2)); + return (fib(a) + fib(b)); } } @@ -49,25 +49,29 @@ class _PerformancePageState extends State { padding: EdgeInsets.only(top: 150), child: Column( children: [ - FutureBuilder( + FutureBuilder( future: computeFuture, builder: (context, snapshot) { return RaisedButton( child: const Text('Compute on Main'), elevation: 8.0, - onPressed: createMainIsolateCallBack(context, snapshot), + onPressed: + snapshot.connectionState == ConnectionState.done + ? () => handleComputeOnMain(context) + : null, ); }, ), - FutureBuilder( + FutureBuilder( future: computeFuture, builder: (context, snapshot) { return RaisedButton( - child: const Text('Compute on Secondary'), - elevation: 8.0, - onPressed: - createSecondaryIsolateCallBack(context, snapshot), - ); + child: const Text('Compute on Secondary'), + elevation: 8.0, + onPressed: + snapshot.connectionState == ConnectionState.done + ? () => handleComputeOnSecondary(context) + : null); }, ), ], @@ -78,54 +82,40 @@ class _PerformancePageState extends State { ); } - VoidCallback createMainIsolateCallBack( - BuildContext context, - AsyncSnapshot snapshot, - ) { - if (snapshot.connectionState == ConnectionState.done) { - return () { - setState(() { - computeFuture = computeOnMainIsolate() - ..then((_) { - final snackBar = SnackBar( - content: Text('Main Isolate Done!'), - ); - Scaffold.of(context).showSnackBar(snackBar); - }); - }); - }; - } else { - return null; - } + void handleComputeOnMain(BuildContext context) { + var future = computeOnMainIsolate() + ..then((_) { + var snackBar = SnackBar( + content: Text('Main Isolate Done!'), + ); + Scaffold.of(context).showSnackBar(snackBar); + }); + + setState(() { + computeFuture = future; + }); } - VoidCallback createSecondaryIsolateCallBack( - BuildContext context, AsyncSnapshot snapshot) { - if (snapshot.connectionState == ConnectionState.done) { - return () { - setState(() { - computeFuture = computeOnSecondaryIsolate() - ..then((_) { - final snackBar = SnackBar( - content: Text('Secondary Isolate Done!'), - ); - Scaffold.of(context).showSnackBar(snackBar); - }); - }); - }; - } else { - return null; - } + void handleComputeOnSecondary(BuildContext context) { + var future = computeOnSecondaryIsolate() + ..then((_) { + var snackBar = SnackBar( + content: Text('Secondary Isolate Done!'), + ); + Scaffold.of(context).showSnackBar(snackBar); + }); + + setState(() { + computeFuture = future; + }); } Future computeOnMainIsolate() async { - // A delay is added here to give Flutter the chance to redraw the UI at least - // once before the computation (which, since it's run on the main isolate, - // will lock up the app) begins executing. - await Future.delayed( - Duration(milliseconds: 100), - () => fib(45), - ); + // A delay is added here to give Flutter the chance to redraw the UI at + // least once before the computation (which, since it's run on the main + // isolate, will lock up the app) begins executing. + await Future.delayed(Duration(milliseconds: 100)); + fib(45); } Future computeOnSecondaryIsolate() async { @@ -141,24 +131,22 @@ class SmoothAnimationWidget extends StatefulWidget { class SmoothAnimationWidgetState extends State with TickerProviderStateMixin { - AnimationController _controller; + AnimationController _animationController; Animation _borderAnimation; @override void initState() { super.initState(); - _controller = AnimationController( - duration: const Duration(seconds: 1), - vsync: this, - ); + _animationController = + AnimationController(duration: const Duration(seconds: 1), vsync: this); _borderAnimation = BorderRadiusTween( - begin: BorderRadius.circular(100.0), - end: BorderRadius.circular(0.0), - ).animate(_controller); + begin: BorderRadius.circular(100.0), + end: BorderRadius.circular(0.0)) + .animate(_animationController); - _controller.repeat(reverse: true); + _animationController.repeat(reverse: true); } @override @@ -192,7 +180,7 @@ class SmoothAnimationWidgetState extends State @override void dispose() { - _controller.dispose(); + _animationController.dispose(); super.dispose(); } } diff --git a/isolate_example/pubspec.lock b/isolate_example/pubspec.lock index d791389ad..f064fbcbc 100644 --- a/isolate_example/pubspec.lock +++ b/isolate_example/pubspec.lock @@ -73,7 +73,7 @@ packages: name: provider url: "https://pub.dartlang.org" source: hosted - version: "3.1.0" + version: "3.1.0+1" quiver: dependency: transitive description: