Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_test/flutter_test.dart';
  4.  
  5. class MyWidget extends StatefulWidget {
  6. @override
  7. _MyWidgetState createState() => _MyWidgetState();
  8. }
  9.  
  10. class _MyWidgetState extends State<MyWidget> {
  11. Future process;
  12. bool showFutureBuilder = false;
  13.  
  14. @override
  15. Widget build(BuildContext context) {
  16. return Column(
  17. children: <Widget>[
  18. FlatButton(
  19. child: const Text('WITH FUTURE EXCEPTION'),
  20. onPressed: () {
  21. processButtonWithFuture(futureWithException);
  22. },
  23. ),
  24. if (showFutureBuilder)
  25. FutureBuilder(
  26. future: process,
  27. builder: (context, snapshot) {
  28. if (snapshot.connectionState == ConnectionState.waiting)
  29. return const Text('WAITING');
  30. if (snapshot.hasError) return const Text('ERROR HAPPENED');
  31. if (snapshot.hasData) return const Text('ALL IS FINE');
  32. },
  33. )
  34. ],
  35. );
  36. }
  37.  
  38. void processButtonWithFuture(Function future) {
  39. setState(() {
  40. showFutureBuilder = true;
  41. process = future();
  42. });
  43. }
  44.  
  45. Future<String> futureWithException() async {
  46. throw const FormatException();
  47. }
  48. }
  49.  
  50. void main() {
  51. testWidgets('with Future finds exception', (WidgetTester tester) async {
  52. await tester.pumpWidget(MaterialApp(home: MyWidget()));
  53.  
  54. var handler = FlutterError.onError;
  55.  
  56. FlutterError.onError = (FlutterErrorDetails errorDetails) {
  57. if (errorDetails.exception is FormatException)
  58. print('YES');
  59. FlutterError.onError = handler;
  60. };
  61.  
  62. await tester.tap(find.text('WITH FUTURE EXCEPTION'));
  63.  
  64. await tester.pump();
  65. await tester.pump();
  66.  
  67. });
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement