network_image_mock icon indicating copy to clipboard operation
network_image_mock copied to clipboard

The following _CastError was thrown resolving an image codec

Open alexgrusu43 opened this issue 3 years ago • 1 comments

I'm trying to test a widget that has a Image.network child.

The following log is provided.

type 'Null' is not a subtype of type 'List<int>' in type cast

When the exception was thrown, this was the stack:
#1      _MockHttpResponse.drain (package:flutter_test/src/_binding_io.dart:364:22)
#2      NetworkImage._loadAsync (package:flutter/src/painting/_network_image_io.dart:99:24)
<asynchronous suspension>
<asynchronous suspension>
(elided 2 frames from dart:async and package:stack_trace)

My test:

testWidgets('My new test', (WidgetTester tester) async {
    mockNetworkImagesFor(() async {
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: ScreenWidget(),
          ),
        ),
      );

      final myImageFinder = find.byKey(const Key('TheImageWidget'));

      expect(myImageFinder, findsOneWidget);

    });
  });

In the above sample of code, ScreenWidget has a child that is a Image.network.

Any thoughts?

alexgrusu43 avatar Nov 16 '21 08:11 alexgrusu43

I'm trying to test a widget that has a Image.network child.

The following log is provided.

type 'Null' is not a subtype of type 'List<int>' in type cast

When the exception was thrown, this was the stack:
#1      _MockHttpResponse.drain (package:flutter_test/src/_binding_io.dart:364:22)
#2      NetworkImage._loadAsync (package:flutter/src/painting/_network_image_io.dart:99:24)
<asynchronous suspension>
<asynchronous suspension>
(elided 2 frames from dart:async and package:stack_trace)

My test:

testWidgets('My new test', (WidgetTester tester) async {
    mockNetworkImagesFor(() async {
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: ScreenWidget(),
          ),
        ),
      );

      final myImageFinder = find.byKey(const Key('TheImageWidget'));

      expect(myImageFinder, findsOneWidget);

    });
  });

In the above sample of code, ScreenWidget has a child that is a Image.network.

Any thoughts?

Put await in front of mockNetworkImagesFor as follows:

await mockNetworkImagesFor(() async { await tester.pumpWidget( MaterialApp( home: Scaffold( body: ScreenWidget(), ), ), ); ...... }

Add00w avatar Dec 08 '21 08:12 Add00w