Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jifalops/network_resource
A Flutter package to automatically cache network resources and use them when offline.
https://github.com/jifalops/network_resource
Last synced: 2 months ago
JSON representation
A Flutter package to automatically cache network resources and use them when offline.
- Host: GitHub
- URL: https://github.com/jifalops/network_resource
- Owner: jifalops
- License: mit
- Created: 2018-06-14T02:07:00.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-08-25T21:18:15.000Z (over 6 years ago)
- Last Synced: 2023-08-20T22:38:32.634Z (over 1 year ago)
- Language: Dart
- Size: 25.4 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# ~~network_resource~~
## Deprecated in favor of [async_resource](https://pub.dartlang.org/packages/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````dart
// 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`.
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` 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](https://pub.dartlang.org/packages/path_provider) package to help specify where the cache file should be located.
```dart
// 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> {
EventListResource()
: super(
url: 'http://example.com/events.json',
cacheFile: File('events.json'),
maxAge: Duration(minutes: 60),
isBinary: false);
@override
List parseContents(contents) {
List events;
json.decode(contents).forEach((item) => events.add(Event(item)));
return events;
}
}final eventsResource = EventListResource();
class _EventListState extends State {
Future 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>(
future: eventsResource.get(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return EventList();
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return Center(child: CircularProgressIndicator());
},
)));
```