"Fossies" - the Fresh Open Source Software Archive

Member "flutter-1.22.4/dev/integration_tests/image_loading/lib/main.dart" (13 Nov 2020, 2431 Bytes) of package /linux/misc/flutter-1.22.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Dart source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2014 The Flutter Authors. All rights reserved.
    2 // Use of this source code is governed by a BSD-style license that can be
    3 // found in the LICENSE file.
    4 
    5 import 'dart:async';
    6 import 'dart:io';
    7 
    8 import 'package:flutter/material.dart';
    9 import 'package:mockito/mockito.dart';
   10 
   11 void main() {
   12   final ThrowingHttpClient httpClient = ThrowingHttpClient();
   13   final HttpOverrides overrides = ProvidedHttpOverrides(httpClient);
   14   HttpOverrides.global = overrides;
   15   final ZoneSpecification specification = ZoneSpecification(
   16     handleUncaughtError:(Zone zone, ZoneDelegate delegate, Zone parent, Object error, StackTrace stackTrace) {
   17     FlutterError.reportError(FlutterErrorDetails(
   18       exception: error,
   19       context: ErrorDescription('In the Zone handleUncaughtError handler'),
   20       silent: false,
   21     ));
   22   });
   23   when(httpClient.getUrl(any)).thenAnswer((Invocation invocation) {
   24     final Completer<HttpClientRequest> completer = Completer<HttpClientRequest>.sync();
   25     completer.completeError(Error());
   26     return completer.future;
   27   });
   28   Zone.current.fork(specification: specification).run<void>(() {
   29     runApp(ImageLoader());
   30   });
   31 }
   32 
   33 class ImageLoader extends StatefulWidget {
   34   @override
   35   _ImageLoaderState createState() => _ImageLoaderState();
   36 }
   37 
   38 class _ImageLoaderState extends State<ImageLoader> {
   39   bool caughtError = false;
   40 
   41   @override
   42   void initState() {
   43     // This is not an image, but we don't care since we're using a faked
   44     // http client.
   45     const NetworkImage image = NetworkImage('https://github.com/flutter/flutter');
   46     final ImageStream stream = image.resolve(ImageConfiguration.empty);
   47     ImageStreamListener listener;
   48     listener = ImageStreamListener(
   49       (ImageInfo info, bool syncCall) {
   50         stream.removeListener(listener);
   51       },
   52       onError: (dynamic error, StackTrace stackTrace) {
   53         print('ERROR caught by framework');
   54         stream.removeListener(listener);
   55       },
   56     );
   57     stream.addListener(listener);
   58     super.initState();
   59   }
   60 
   61   @override
   62   Widget build(BuildContext context) {
   63     return const Text('hello', textDirection: TextDirection.ltr);
   64   }
   65 }
   66 
   67 class ThrowingHttpClient extends Mock implements HttpClient {}
   68 
   69 class ProvidedHttpOverrides extends HttpOverrides {
   70   ProvidedHttpOverrides(this.httpClient);
   71 
   72   final ThrowingHttpClient httpClient;
   73   @override
   74   HttpClient createHttpClient(SecurityContext context) {
   75     return httpClient;
   76   }
   77 }