Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'package:flutter/material.dart';
- import 'package:flutter_test/flutter_test.dart';
- class MyWidget extends StatefulWidget {
- @override
- _MyWidgetState createState() => _MyWidgetState();
- }
- class _MyWidgetState extends State<MyWidget> {
- Future process;
- bool showFutureBuilder = false;
- @override
- Widget build(BuildContext context) {
- return Column(
- children: <Widget>[
- FlatButton(
- child: const Text('WITH FUTURE EXCEPTION'),
- onPressed: () {
- processButtonWithFuture(futureWithException);
- },
- ),
- if (showFutureBuilder)
- FutureBuilder(
- future: process,
- builder: (context, snapshot) {
- if (snapshot.connectionState == ConnectionState.waiting)
- return const Text('WAITING');
- if (snapshot.hasError) return const Text('ERROR HAPPENED');
- if (snapshot.hasData) return const Text('ALL IS FINE');
- },
- )
- ],
- );
- }
- void processButtonWithFuture(Function future) {
- setState(() {
- showFutureBuilder = true;
- process = future();
- });
- }
- Future<String> futureWithException() async {
- throw const FormatException();
- }
- }
- void main() {
- testWidgets('with Future finds exception', (WidgetTester tester) async {
- await tester.pumpWidget(MaterialApp(home: MyWidget()));
- var handler = FlutterError.onError;
- FlutterError.onError = (FlutterErrorDetails errorDetails) {
- if (errorDetails.exception is FormatException)
- print('YES');
- FlutterError.onError = handler;
- };
- await tester.tap(find.text('WITH FUTURE EXCEPTION'));
- await tester.pump();
- await tester.pump();
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement