async_field 1.0.2 copy "async_field: ^1.0.2" to clipboard
async_field: ^1.0.2 copied to clipboard

outdated

Async fields that can be stored or fetched from any source (databases, web services, local storage or other thread/isolate), with observable values, caches and stale versions.

async_field #

pub package Null Safety Codecov CI GitHub Tag New Commits Last Commits Pull Requests Code size License FOSSA Status

Async fields that can be stored or fetched from any source (databases, web services, local storage or other thread/isolate), with observable values, caches and stale versions.

Usage #

A simple usage example:

import 'dart:convert' as convert;
import 'dart:io';

import 'package:async_field/async_field.dart';

void main() async {
  // The fields storage:
  var storage = AsyncStorage();

  // Get field 'btc_usd',
  var field = storage.getField<double>('btc_usd')
    ..defaultValue = double.nan // default value (before fetch).
    ..withFetcher(_fetchBtcUsd) // the field fetcher.
    ..onChange.listen((field) => print('onChange> $field')); // change listener.

  print('BTX-USD: $field');
  print('field.info: ${field.info}');

  // Get the field value:
  var btcUsd = await field.get();

  print('field.get(): $btcUsd');

  print('BTX-USD: $field');
  print('field.info: ${field.info}');
}

/// Function that fetches the BTS-USD price.
Future<double> _fetchBtcUsd(AsyncField<double> field) async {
  return _getURL('https://api.coindesk.com/v1/bpi/currentprice.json')
          .resolveMapped((body) {
    var json = convert.json.decode(body) as Map<String, dynamic>;
    var rate = json['bpi']['USD']['rate_float'];
    return double.parse('$rate');
  });
}

/// Simple HTTP get URL function.
Future<String> _getURL(String url) async {
  var uri = Uri.parse(url);
  var httpClient = HttpClient();

  var response =
  await httpClient.getUrl(uri).then((request) => request.close());

  var data = await response.transform(convert.Utf8Decoder()).toList();
  var body = data.join();
  return body;
}

OUTPUT:

BTX-USD: NaN
field.info: { "id": "btc_usd" , "storage": 1 }
field.get(): 34498.9417
BTX-USD: 34498.9417
field.info: { "value": 34498.9417 , "id": "btc_usd" , "valueTime": 1624846823650 , "storage": 1 }
onChange> 34498.9417

Source #

The official source code is hosted @ GitHub:

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Contribution #

Any help from the open-source community is always welcome and needed:

  • Found an issue?
    • Please fill a bug report with details.
  • Wish a feature?
    • Open a feature request with use cases.
  • Are you using and liking the project?
    • Promote the project: create an article, do a post or make a donation.
  • Are you a developer?
    • Fix a bug and send a pull request.
    • Implement a new feature.
    • Improve the Unit Tests.
  • Have you already helped in any way?
    • Many thanks from me, the contributors and everybody that uses this project!

If you donate 1 hour of your time, you can contribute a lot, because others will do the same, just be part and start with your 1 hour.

Author #

Graciliano M. Passos: gmpassos@GitHub.

License #

Apache License - Version 2.0

FOSSA Status

5
likes
0
pub points
74%
popularity

Publisher

unverified uploader

Async fields that can be stored or fetched from any source (databases, web services, local storage or other thread/isolate), with observable values, caches and stale versions.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

async_extension, collection

More

Packages that depend on async_field