HarperDB

Pub version GitHub all releases language lICENCE

A package for connecting Flutter with HarperDB. Its only dependency is the http package by dart-lang.

Installation


Open your project and type the code below in your terminal

flutter pub add harperdb

Usage


Import the package

import 'package:harperdb/harperdb.dart';

Set your HDB_URL, HDB_USER, and HDB_PASSWORD variables for use within your application:

const HDB_URL = 'https://hdb-publicdemo.harperdbcloud.com';
const HDB_USER = 'HDB_READER';
const HDB_PASSWORD = 'password';

Build your function (must have async type of Future):

  // Function must be async
  // Use <List> return for queries.
  // Use <Map> for inserts, updates, deletes, etc.
  
  Future<List> loadData() async {
    var show = await harperDB(
      HDB_URL,
      HDB_USER,
      HDB_PASSWORD,
      {
        // Contains the syntax code from HarperDB for operations.
        "operation": "sql",
        "sql": "select * from dev.dog"
      },
    );
    //this shows you if the query ran properly
    debugPrint(
      show.toString(),
    );
    //set the result of the query to your list which will be returned
    setState(() {
      harperData = show;
    });
    // the function type which was stated must be returned
    return harperData;
  }

Call your function on init:

  @override
  void initState() {
    queryHarperDB = loadData();
    super.initState();
  }

To display the results of your query, use a FutureBuilder to ensure the data has returned from the API:

  @override
  Widget build(BuildContext context) {
      return Scaffold(
        body: FutureBuilder(
          future: queryHarperDB,
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting) {
              return const Center(child: CircularProgressIndicator());
            }
            else if (snapshot.connectionState == ConnectionState.done) {
              return ListView.builder(
                itemCount: harperData.length,
                itemBuilder: (context, index) {
                  return ListTile(
                    leading: Text(
                      harperData[index]['dog_name'],
                    ),
                    title: Text(
                      harperData[index]['owner_name'],
                      textAlign: TextAlign.center,
                    ),
                    trailing: Text(
                      harperData[index]['dog_age'].toString(),
                    ),
                  );
                },
              );
            }
            else {
              return const Center(
                  child: Text("Something went wrong."));
            }
          },
        ),
      );
      }
  }

NOTE :

The return type of your function is dependent on the HarperDB operation you are executing. Queries will return a <List>, while inserts, updates, deletes, etc. will return a <Map>

Libraries

harperdb