Search categories, packages and repositories

Package detailsv2.1.1

network_image_mock

Utility for providing mocked Image.network response in Flutter widget tests.

AndroidiOSmacOSWindowsLinux
30-day downloads
272K
Likes
66
Pub points
160

Install package

flutter pub add network_image_mock

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

A utility package for mocking Image.network responses in Flutter widget tests. It allows developers to simulate network image loading without making actual HTTP requests, ensuring consistent and reliable test results. Ideal for testing UI components that display images from remote sources, it provides a simple way to return placeholder images or predefined assets during testing. The package is lightweight and integrates seamlessly with Flutter's testing framework.

Use cases

  • Testing widgets that display remote images
  • Simulating image loading states
  • Avoiding network calls in unit tests
  • Validating image placeholders
  • Ensuring test consistency

Key features

  • Mock Image.network responses
  • Supports placeholder images
  • No external network dependencies
  • Easy integration with test files
  • Works across all Flutter platforms

Suitable for

  • Flutter developers writing widget tests
  • Teams requiring stable test environments
  • Projects with frequent UI changes involving images
  • Test automation engineers
  • CI/CD pipelines

Considerations

  • Not intended for production use
  • Requires manual setup in test files
  • Limited to image mocking scenarios
  • Does not handle image caching
  • Must be removed from production dependencies

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

Network Image Mock

Pub Version Lint & Test codecov.io Null safety No null safety GitHub Repo stars GitHub contributors GitHub code size in bytes GitHub

A utility for providing mocked response to Image.network in Flutter widget tests.

Introduction

Since you are here you probably already know that calling Image.network results in 400 response in Flutter widget tests. The reason for this is that default HTTP client in tests always return a 400.

So, what can we do about it? Instead of copying the whole code over and over again for mocking the HTTP client, I created this package. It is heavily inspired by roughike/image_test_utils, however that package is not being maintained despite multiple pull requests asking for bumping the mockito version and making the package usable again.

Installing

Null safety

This package should be installed under dev_dependencies with

dev_dependencies:
  network_image_mock: ^2.0.1
Without null safety (deprecated)

If you have legacy unmigrated code that does not use Null safety, install under dev_dependencies with

dev_dependencies:
  network_image_mock: 1.1.0

Example

The package is quite straightforward to use. All you have to do is include it in your test file and wrap widget testing functions that require proper Image.network response in mockNetworkImagesFor() function provided by this package. A full test example could look like this.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:network_image_mock/network_image_mock.dart';

Widget makeTestableWidget() => MaterialApp(home: Image.network(''));

void main() {
  testWidgets(
    'should properly mock Image.network and not crash',
    (WidgetTester tester) async {
      mockNetworkImagesFor(() => tester.pumpWidget(makeTestableWidget()));
    },
  );
}

This is actually an example taken from tests for this package.

Contributing

There is not much to contribute since the package serves its purpose, however, in chance of needing to bump or adjust some version, or any other suggestion for that matter, please read CONTRIBUTING.

Categories

Dependencies

Dependency list 1 items

Source repository

Projects using this package