Giter Site home page Giter Site logo

radikz / cache_data Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 109 KB

A Flutter library to fetch data then save the cache using hive; return dart objects

Home Page: https://pub.dev/packages/cache_data

License: MIT License

Kotlin 0.65% Swift 2.12% Objective-C 0.20% Dart 87.49% HTML 9.54%
cache flutter-package

cache_data's Introduction

cache_data

A Flutter library to fetch data then save the data with duration as a age of data using hive; return dart objects. This library depend on hive for the database and dart_json_mapper for deserialize JSON to Dart object

Getting started

Add the dependency to pubspec.yaml:

dependencies:
  cache_data:
  dart_json_mapper:
  dart_json_mapper_flutter:
dev_dependencies:
  build_runner:

Put @jsonSerializable at your model.

data.dart

@jsonSerializable
class Data {
  int userId;
  int id;
  String title;

  Data({
    required this.userId,
    required this.id,
    required this.title,
  });
}

Next is add main.mapper.g.dart and initializes package.

main.dart

import 'main.mapper.g.dart' show initializeJsonMapper;

import 'package:dart_json_mapper_flutter/dart_json_mapper_flutter.dart' show flutterAdapter;

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await CacheData.init();
  initializeJsonMapper(adapters: [flutterAdapter]);
  runApp(const MyApp());
}

Next, create the build.yaml in project directory.

targets:
  $default:
    builders:
      dart_json_mapper:
        generate_for:
        # here should be listed entry point files having 'void main()' function
          - lib/main.dart

      # This part is needed to tell original reflectable builder to stay away
      # it overrides default options for reflectable builder to an **empty** set of files
      reflectable:
        generate_for:
          - no/files

Now generate main.mapper.g.dart files

pub run build_runner build --delete-conflicting-outputs

or

pub run build_runner watch --delete-conflicting-outputs

if you want to generate files everytime you make changes in the code.

Usage

final cache = CacheData<List<Data>>();

FutureBuilder<List<Data>?>(
  future: cache.fetchData(
            'https://jsonplaceholder.typicode.com/albums',
            duration: duration),
  builder: (context, snapshot) {
    ...
  },
)

Additional information

For more details about how to use dart_json_mapper, you can visit this package

cache_data's People

Contributors

radikz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.