harperdb

Last updated:

0 purchases

harperdb Image
harperdb Images
Add to Cart

Description:

harperdb

HarperDB #

#
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
copied to clipboard
Usage #

Import the package
import 'package:harperdb/harperdb.dart';
copied to clipboard
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';
copied to clipboard
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;
}
copied to clipboard
Call your function on init:
@override
void initState() {
queryHarperDB = loadData();
super.initState();
}
copied to clipboard
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."));
}
},
),
);
}
}
copied to clipboard
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>

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.