Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jifalops/async_resource

Automatically cache network resources and use them when offline. Interface with local resources on any platform.
https://github.com/jifalops/async_resource

Last synced: about 2 months ago
JSON representation

Automatically cache network resources and use them when offline. Interface with local resources on any platform.

Awesome Lists containing this project

README

        

# async_resource

Automatically cache network resources and use them when offline. Interface with local resources on any platform.

## Examples

See also the [working example](https://github.com/jifalops/async_resource/tree/master/example).

### Wrapping in a stream

```dart
// `res` is any AsyncResource.
final resource = StreamedResource(res);
resource.sink.add(false);
```

### Flutter and native

Import `FileResource`.

```dart
import 'package:async_resource/file_resource.dart';
```

Define a resource.

```dart
// Flutter needs a valid directory to write to.
// `getApplicationDocumentsDirectory()` is in the `path_provider` package.
// Native applications do not need this step.
final path = (await getApplicationDocumentsDirectory()).path;

final myDataResource = HttpNetworkResource(
url: 'https://example.com/my-data.json',
parser: (contents) => MyData.fromJson(contents),
cache: FileResource(File('$path/my-data.json')),
maxAge: Duration(minutes: 60),
strategy: CacheStrategy.cacheFirst,
);
```

Basic usage

```dart
final myData = await myDataResource.get();
// or without `await`
myDataResource.get().then((myData) => print(myData));
```

Flutter pull-to-refresh example

```dart
class MyDataView extends StatefulWidget {
@override
_MyDataViewState createState() => _MyDataViewState();
}

class _MyDataViewState extends State {
bool refreshing = false;

@override
Widget build(BuildContext context) {
return Scaffold(
body: RefreshIndicator(
onRefresh: refresh,
child: FutureBuilder(
future: myDataResource.get(forceReload: refreshing),
initialData: myDataResource.data,
builder: (context, snapshot) {
if (snapshot.hasData) {
return _buildView(snapshot.data);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return Center(child: CircularProgressIndicator());
},
)));
}

Future refresh() async {
setState(() => refreshing = true);
refreshing = false;
}
}
```

### Flutter Using MMKV

MMKV used is from https://pub.dartlang.org/packages/flutter_mmkv

MMKV is really fast and can get the data stored in just 3-5 milliseconds, really fast.

import `MMKVResource`.

```dart
import 'package:async_resource/mmkv_resource.dart';
```

Define a resource.

```dart
// Flutter needs a valid directory to write to.
// `getApplicationDocumentsDirectory()` is in the `path_provider` package.
// Native applications do not need this step.
final path = (await getApplicationDocumentsDirectory()).path;

final myDataResource = HttpNetworkResource(
url: 'https://example.com/my-data.json',
parser: (contents) => MyData.fromJson(contents),
cache: MMKVResource('my_key','$path/my_key'),
maxAge: Duration(minutes: 60),
strategy: CacheStrategy.cacheFirst,
);
```

### Flutter using Shared Preferences

Import `SharedPrefsResource`.

```dart
import 'package:shared_prefs_resource/shared_prefs_resource.dart';
```

Definition

```dart
final themeResource = StringPrefsResource('theme');
```

Usage example

```dart
class ThemedApp extends StatefulWidget {
@override
State createState() => _ThemedAppState();
}

class _ThemedAppState extends BlocState {
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: themeResource.get(),
initialData: themeResource.data,
builder: (context, snapshot) {
if (snapshot.hasData) {
return MaterialApp(
title: 'My themed app',
theme: buildTheme(snapshot.data),
home: HomePage());
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return Center(child: CircularProgressIndicator());
},
);
}
}
```

### Web using service worker

Import browser-based resources.

```dart
import 'package:async_resource/browser_resource.dart';
```

Define the resource.

```dart
final myDataResource = ServiceWorkerResource(
cache: ServiceWorkerCacheEntry(
name: config.cacheName,
url: 'https://example.com/my-data.json',
parser: (contents) => MyData.fromJson(contents),
maxAge: Duration(minutes: 60)));
```

Usage

```dart
myDataResource.get();
```

### Web using local/session storage

Import browser-based resources.

```dart
import 'package:async_resource/browser_resource.dart';
```

Define

```dart
final themeResource = StorageEntry('theme');
final sessionResource = StorageEntry('token', type: StorageType.sessionStorage);
```

Use

```dart
themeResource.get();
sessionResource.get();
```