From 792e7ce872354ea86d5ac2020a631a205cb4f48f Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Tue, 28 Nov 2017 11:00:36 -0800 Subject: [PATCH] add an assert to validate the RefreshIndicator.onRefresh result (#13198) * add an assert to validate the RefreshIndicator.onRefresh result * add a test for RefreshIndicator.onRefresh assert * switch to using FlutterError.reportError, FlutterErrorDetails, and FlutterError --- .../lib/src/material/refresh_indicator.dart | 17 +++++++++- .../test/material/refresh_indicator_test.dart | 31 ++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/material/refresh_indicator.dart b/packages/flutter/lib/src/material/refresh_indicator.dart index c936643613..79ade3b61a 100644 --- a/packages/flutter/lib/src/material/refresh_indicator.dart +++ b/packages/flutter/lib/src/material/refresh_indicator.dart @@ -326,7 +326,22 @@ class RefreshIndicatorState extends State with TickerProviderS _mode = _RefreshIndicatorMode.refresh; }); - widget.onRefresh().whenComplete(() { + final Future refreshResult = widget.onRefresh(); + assert(() { + if (refreshResult == null) + FlutterError.reportError(new FlutterErrorDetails( + exception: new FlutterError( + 'The onRefresh callback returned null.\n' + 'The RefreshIndicator onRefresh callback must return a Future.' + ), + context: 'when calling onRefresh', + library: 'material library', + )); + return true; + }); + if (refreshResult == null) + return; + refreshResult.whenComplete(() { if (mounted && _mode == _RefreshIndicatorMode.refresh) { completer.complete(); _dismiss(_RefreshIndicatorMode.done); diff --git a/packages/flutter/test/material/refresh_indicator_test.dart b/packages/flutter/test/material/refresh_indicator_test.dart index f73cd753e4..3f216ede15 100644 --- a/packages/flutter/test/material/refresh_indicator_test.dart +++ b/packages/flutter/test/material/refresh_indicator_test.dart @@ -46,7 +46,7 @@ void main() { await tester.pump(const Duration(seconds: 1)); // finish the indicator hide animation expect(refreshCalled, true); }); - + testWidgets('Refresh Indicator - nested', (WidgetTester tester) async { refreshCalled = false; await tester.pumpWidget( @@ -349,4 +349,33 @@ void main() { expect(completed1, true); expect(completed2, true); }); + + testWidgets('RefreshIndicator - onRefresh asserts', (WidgetTester tester) async { + refreshCalled = false; + await tester.pumpWidget( + new MaterialApp( + home: new RefreshIndicator( + onRefresh: () { + refreshCalled = true; + // Missing a returned Future value here. + }, + child: new ListView( + physics: const AlwaysScrollableScrollPhysics(), + children: ['A', 'B', 'C', 'D', 'E', 'F'].map((String item) { + return new SizedBox( + height: 200.0, + child: new Text(item), + ); + }).toList(), + ), + ), + ), + ); + + await tester.fling(find.text('A'), const Offset(0.0, 300.0), 1000.0); + await tester.pump(); + await tester.pump(const Duration(seconds: 1)); // finish the scroll animation + expect(refreshCalled, true); + expect(tester.takeException(), isFlutterError); + }); }