network_resource 0.2.5

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

network_resource

Deprecated in favor of async_resource.

Automatically cache network resources and use them when offline.

NetworkResource fetches data over HTTP, caches it in a file, and holds it in memory. The main method, get(), will return the value in memory, cache, or fetch from the network -- in that order. If the cache file is older than maxAge, the cache will be updated from the network if available. To manually refresh, use get(forceReload: true) or getFromNetwork(). The latter can be used to avoid cache fallback.

Examples

There are three concrete classes included.

  • StringNetworkResource
  • StringListNetworkResource
  • BinaryNetworkResource
// String data.
final eventsResource = StringNetworkResource(
  url: 'https://example.com/events.json',
  cacheFile: File('events.json'),
  maxAge: Duration(minutes: 60),
);

// Binary data.
final photo = BinaryNetworkResource(
  url: 'https://example.com/photo.png',
  cacheFile: File('photo.png'),
  maxAge: Duration(hours: 24),
);

// A string list, line by line.
 final words = StringListNetworkResource(
  url: 'https://example.com/wordlist.txt',
  cacheFile: File('wordlist.txt'),
);

// Parsing a JSON string into a `List<Event>`.
json.decode(data).forEach((item) => events.add(Event(item)));

Extend

Instead of declaring the resource and parsing its data separately, extend the base class and implement parseContents(contents) where either a String or List<int> will be passed, depending on the value of isBinary.

While this example uses Flutter, the package is not dependent on Flutter. If you are using Flutter, try the path provider package to help specify where the cache file should be located.

// This example subclasses `NetworkResource` to manage fetching and parsing
// an event list in JSON format. Items are shown in a list with pull-to-refresh.

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:network_resource/network_resource.dart';

class Event {
  final String title;
  Event(this.title);
}

class EventListResource extends NetworkResource<List<Event>> {
  EventListResource()
      : super(
            url: 'http://example.com/events.json',
            cacheFile: File('events.json'),
            maxAge: Duration(minutes: 60),
            isBinary: false);
  @override
  List<Event> parseContents(contents) {
    List events;
    json.decode(contents).forEach((item) => events.add(Event(item)));
    return events;
  }
}

final eventsResource = EventListResource();

class _EventListState extends State<EventList> {
  Future<Null> refresh() async {
    await eventsResource.get(forceReload: true);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: RefreshIndicator(
            onRefresh: refresh,
            child: ListView.builder(
                itemBuilder: (context, index) =>
                    Text(eventsResource.data[index].title))));
  }
}

class EventList extends StatefulWidget {
  @override
  _EventListState createState() => _EventListState();
}

void main() => runApp(MaterialApp(
    title: 'Network Resource example',
    home: FutureBuilder<List<Event>>(
      future: eventsResource.get(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return EventList();
        } else if (snapshot.hasError) {
          return Text('${snapshot.error}');
        }
        return Center(child: CircularProgressIndicator());
      },
    )));

[0.2.5] - August 25, 2018

  • Deprecated in favor of async_resource.

[0.2.4] - July 21, 2018

  • Downgrade dependency due to conflict with flutter_test.

[0.2.2] - July 21, 2018

  • Make dart-pub happy.
  • More documentation and tests.

[0.2.1] - July 20, 2018

  • Fix missing dependency.

[0.2.0] - July 20, 2018

  • Breaking: a File must be passed instead of a file name.
  • Removed Flutter dependency.
  • Added unit tests.

[0.1.0] - July 15, 2018

  • Make NetworkResource easily extendable so that it can be adapted to any data type.

[0.0.4] - July 15, 2018

  • Return data without waiting for cache write to finish.
  • Many documentation improvements.

[0.0.3] - June 20, 2018

  • Breaking: NetworkResource is now abstract. You must use StringNetworkResource, StringListNetworkResource, BinaryNetworkResource, or extend and create your own.

[0.0.2] - June 13, 2018

  • Added example usage.

[0.0.1] - June 13, 2018

  • First release.

example/main.dart

import 'dart:convert';
import 'dart:io';
import 'package:network_resource/network_resource.dart';

// A string list, line by line.
final words = StringListNetworkResource(
  url: 'https://example.com/wordlist.txt',
  cacheFile: File('wordlist.txt'),
);

// Extending [NetworkResource] to provide a different data type.
class EventListResource extends NetworkResource<List<Event>> {
  EventListResource()
      : super(
            url: 'http://example.com/events.json',
            cacheFile: File('events.json'),
            maxAge: Duration(minutes: 60),
            isBinary: false);
  @override
  List<Event> parseContents(contents) {
    List events;
    json.decode(contents).forEach((item) => events.add(Event(item)));
    return events;
  }
}

class Event {
  final String title;
  Event(this.title);
}

Use this package as a library

1. Depend on it

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


dependencies:
  network_resource: ^0.2.5

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:


import 'package:network_resource/network_resource.dart';
  
Version Uploaded Documentation Archive
0.2.5 Aug 25, 2018 Go to the documentation of network_resource 0.2.5 Download network_resource 0.2.5 archive
0.2.4 Jul 21, 2018 Go to the documentation of network_resource 0.2.4 Download network_resource 0.2.4 archive
0.2.3 Jul 21, 2018 Go to the documentation of network_resource 0.2.3 Download network_resource 0.2.3 archive
0.2.2 Jul 21, 2018 Go to the documentation of network_resource 0.2.2 Download network_resource 0.2.2 archive
0.2.1 Jul 21, 2018 Go to the documentation of network_resource 0.2.1 Download network_resource 0.2.1 archive
0.2.0 Jul 20, 2018 Go to the documentation of network_resource 0.2.0 Download network_resource 0.2.0 archive
0.1.0 Jul 15, 2018 Go to the documentation of network_resource 0.1.0 Download network_resource 0.1.0 archive
0.0.4 Jul 15, 2018 Go to the documentation of network_resource 0.0.4 Download network_resource 0.0.4 archive
0.0.3 Jun 20, 2018 Go to the documentation of network_resource 0.0.3 Download network_resource 0.0.3 archive
0.0.2 Jun 14, 2018 Go to the documentation of network_resource 0.0.2 Download network_resource 0.0.2 archive

All 11 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
44
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
72
Learn more about scoring.

We analyzed this package on Dec 5, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7

Platforms

Detected platforms: Flutter, other

Primary library: package:network_resource/network_resource.dart with components: io.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.19.0 <3.0.0
http ^0.11.3+16 0.11.3+17 0.12.0
meta ^1.1.5 1.1.6
path ^1.6.1 1.6.2
Transitive dependencies
async 2.0.8
charcode 1.1.2
collection 1.14.11
http_parser 3.1.3
source_span 1.4.1
string_scanner 1.0.4
typed_data 1.1.6
Dev dependencies
mockito ^3.0.0-beta+3
test ^1.3.0