network_to_file_image 1.0.4

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 84

network_to_file_image #

This is a mixture of [FileImage] and [NetworkImage]. It will download the image from the url once, save it locally in the file system, and then use it from there in the future.

In more detail:

Given a file and url of an image, it first tries to read it from the local file. It decodes the given [File] object as an image, associating it with the given scale.

However, if the image doesn't yet exist as a local file, it fetches the given URL from the network, associating it with the given scale, and then saves it to the local file. The image will be cached regardless of cache headers from the server.

Notes:

  • If the provided url is null or empty, [NetworkToFileImage] will default to [FileImage]. It will read the image from the local file, and won't try to download it from the network.

  • If the provided file is null, [NetworkToFileImage] will default to [NetworkImage]. It will download the image from the network, and won't save it locally.

Usage #

Import the package #

First, add network_to_file_image as a dependency in your pubspec.yaml

Then, import it:

import 'package:network_to_file_image/network_to_file_image.dart';

Use the package #

If you also listed path_provider in your pubspec.yaml file:

path_provider: ^0.4.1

Then you can create a file from a file name:

Future<File> file(String filename) async {
  Directory dir = await getApplicationDocumentsDirectory();
  String pathName = p.join(dir.path, filename);
  return File(pathName);
}

var myFile = await file("myFileName.png"),

Then, create the image:

Image(image: NetworkToFileImage(url: "http://example.com/someFile.png", file: myFile))

If you make debug=true it prints to the console whether the image was read from the file or fetched from the network:

Image(image: NetworkToFileImage(url: "http://example.com/someFile.png", file: myFile, debug: true))    

Don't forget to check the example tab.

Tests #

You can set mock files. Please see methods:

  • setMockFile(File file, Uint8List bytes)
  • setMockUrl(String url, Uint8List bytes)
  • clearMocks()
  • clearMockFiles()
  • clearMockUrls()

See also #

[0.0.1] - 10/12/2018

  • Reads from the file, or fetches from the network and saves to the file.

[0.0.2] - 10/12/2018

  • Corrected changelog and license.

[1.0.0] - 11/12/2018

  • Tested thoroughly.

[1.0.1] - 04/01/2018

  • Allow mock files.

[1.0.2] - 04/01/2018

  • Details.

[1.0.3] - 16/01/2018

  • Allow mock urls.

[1.0.4] - 22/02/2018

  • Small fix.

example/main.dart

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:network_to_file_image/network_to_file_image.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';

String flutterLogoUrl =
    "https://upload.wikimedia.org/wikipedia/commons/1/17/Google-flutter-logo.png";

String flutterLogoFileName = "flutter.png";

void main() async {
  runApp(
    MaterialApp(
      home: Demo(
        url: flutterLogoUrl,
        file: await file(flutterLogoFileName),
      ),
    ),
  );
}

Future<File> file(String filename) async {
  Directory dir = await getApplicationDocumentsDirectory();
  String pathName = p.join(dir.path, filename);
  return File(pathName);
}

class Demo extends StatelessWidget {
  final String url;
  final File file;

  const Demo({Key key, this.url, this.file}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    //
    return Scaffold(
      appBar: AppBar(title: const Text('Network to file image example')),
      body: Padding(
        padding: const EdgeInsets.all(30.0),
        child: Image(
          image: NetworkToFileImage(url: url, file: file, debug: true),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  network_to_file_image: ^1.0.4

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:network_to_file_image/network_to_file_image.dart';
  
Version Uploaded Documentation Archive
1.0.4 Feb 22, 2019 Go to the documentation of network_to_file_image 1.0.4 Download network_to_file_image 1.0.4 archive
1.0.3 Jan 16, 2019 Go to the documentation of network_to_file_image 1.0.3 Download network_to_file_image 1.0.3 archive
1.0.2 Jan 4, 2019 Go to the documentation of network_to_file_image 1.0.2 Download network_to_file_image 1.0.2 archive
1.0.1 Jan 4, 2019 Go to the documentation of network_to_file_image 1.0.1 Download network_to_file_image 1.0.1 archive
1.0.0 Dec 11, 2018 Go to the documentation of network_to_file_image 1.0.0 Download network_to_file_image 1.0.0 archive
0.0.2 Dec 10, 2018 Go to the documentation of network_to_file_image 0.0.2 Download network_to_file_image 0.0.2 archive
0.0.1 Dec 10, 2018 Go to the documentation of network_to_file_image 0.0.1 Download network_to_file_image 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
70
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
95
Overall:
Weighted score of the above. [more]
84
Learn more about scoring.

We analyzed this package on Jun 17, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.2
  • pana: 0.12.18
  • Flutter: 1.5.4-hotfix.2

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/network_to_file_image.dart.

Run flutter format to format lib/network_to_file_image.dart.

Maintenance issues and suggestions

Support latest dependencies. (-5 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
path_provider ^0.4.1