Giter Site home page Giter Site logo

md-siam / hive_crud Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 1.0 3.5 MB

This repo will teach you how to use hive package for storing data locally in your device. Hive is a lightweight and blazing fast key-value database written in pure Dart.

License: MIT License

Kotlin 1.26% Swift 4.03% Objective-C 0.38% Dart 80.83% Ruby 13.50%

hive_crud's Introduction

           

Hive CRUD - KindaCode

Introduction to Hive: This repo will teach you how to use hive package for storing data locally in your device. Hive is a lightweight and blazing fast key-value database written in pure Dart.

To use hive, add the following packages to your pubspec.yaml file:

dependencies:
  # For NoSQL Database
  hive: ^2.2.3
  # Extension for hive
  hive_flutter: ^1.1.0

dev_dependencies:
  # For hive
  hive_generator: ^1.1.3
  build_runner: ^2.2.0

App Demo

Hive CRUD

Create: home_page.dart

  List<Map<String, dynamic>> _items = [];
  final _shoppingBox = Hive.box('shopping_box');

  // Create new item
  Future<void> _createItem(Map<String, dynamic> newItem) async {
    await _shoppingBox.add(newItem);
    _refreshItems(); // update the UI
  }

Read: home_page.dart

  // Get all items from the database
  void _refreshItems() {
    final data = _shoppingBox.keys.map((key) {
      final value = _shoppingBox.get(key);
      return {"key": key, "name": value["name"], "quantity": value['quantity']};
    }).toList();

    setState(() {
      _items = data.reversed.toList();
      // we use "reversed" to sort items in order from the latest to the oldest
    });
  }

  // Retrieve a single item from the database by using its key
  // Our app won't use this function but I put it here for your reference
  // ignore: unused_element
  Map<String, dynamic> _readItem(int key) {
    final item = _shoppingBox.get(key);
    return item;
  }

Update: home_page.dart

  // Update a single item
  Future<void> _updateItem(int itemKey, Map<String, dynamic> item) async {
    await _shoppingBox.put(itemKey, item);
    _refreshItems(); // Update the UI
  }

Delete: home_page.dart

  // Delete a single item
  Future<void> _deleteItem(int itemKey) async {
    await _shoppingBox.delete(itemKey);
    _refreshItems(); // update the UI

    // Display a snackbar
    // ignore: use_build_context_synchronously
    ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('An item has been deleted')));
  }

File Pattern Inside The lib Folder

lib
├── views
│   └── home_page.dart
└── main.dart

hive_crud's People

Contributors

md-siam avatar

Stargazers

 avatar Fakhrul Ramadhan Susanto avatar  avatar  avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

fakhrulramadhan

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.