I want a very SIMPLE way of populating my DataTable dynamically and being able to refresh it when new data is added so it updates & rebuilds instantly. Using Hive boxes and in the simplest fashion possible.
I am using Hive encrypted boxes, but that does not even matter
I am posing this question with an answer below. I spent a ton of time to discover this, as I couldn't find anything else similar using Hive boxes & SIMPLE. I truly hope this helps others, I've gotten tons of help on SO as a green dev. I am quite proud that I could possibly return the favours.
I have a class with adapter hooked up & registered to Hive
import 'package:hive/hive.dart';
part 'person.g.dart';
@HiveType(typeId: 0)
class Person {
@HiveField(0)
final String firstName;
@HiveField(1)
final String lastName;
@HiveField(2)
final int age;
@HiveField(3)
final String Status;
Person(
this.firstName,
this.lastName,
this.age,
this.status,
);
@override
String toString() {
return '{${this.firstName}, ${this.lastName}, ${this.age}, ${this.status}}';
}
}
Saving to the hive on button press
onPressed: () {
final newPersonData = Person(
_firstName,
_lastName,
int.parse(_age),
_status,
);
addPerson(newPersonData);
var box = Hive.box(personTable);
for (var index in box.values) {
print(index);
}
},
DataTable build method
_buildDataTable() {
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: DataTable(
columns: const <DataColumn>[
DataColumn(
label: Text('First'),
),
DataColumn(
label: Text('Last'),
),
DataColumn(
label: Text('Age'),
),
DataColumn(
label: Text('Status'),
),
],
rows: List<DataRow>
// How to dynamically load cells in a ***SIMPLE*** manner?
);
},
),
),
);
}