I am using hive as my NoSQL local database in my flutter app.
Following is my Hive Class:
import 'dart:convert';
import 'package:hive/hive.dart';
import 'package:lpa_exam/src/model/listofexams.dart';
import 'package:lpa_exam/src/model/profile.dart';
part 'hiveprofile.g.dart';
@HiveType()
class PersonModel extends HiveObject{
@HiveField(0)
String language;
@HiveField(1)
String examName;
@HiveField(2)
int examId;
@HiveField(3)
Profile profile;
@HiveField(4)
ListExam listexam;
@override
String toString() {
return jsonEncode({
'language': language,
'examName': this.examName,
'examId': examId,
'profile': profile,
'listexam': listexam
});
}
PersonModel(
this.language, this.examName, this.examId, this.profile, this.listexam);
}
So, my requirement is that on every successful login I am supposed to update profile object. But for that, I have to set all of the others also.
How can I just update the profile object only?
Code:
_personBox = Hive.openBox('personBox');
await _personBox.then((item) {
if (!item.isEmpty) {
print('empty');
item.putAt(0, PersonModel(...,..,..,..,...,..));
}
});
I am using hive version 1.2.0
.
Reference: https://resocoder.com/2019/09/30/hive-flutter-tutorial-lightweight-fast-database/
Just use the putAt()
method, like this:
Hive.box('products').putAt(productIndex, _product);
You can get the productIndex
by using the index from the listView.Builder
like this:
ListView.builder(
itemBuilder: (cxt, i) {
return Row(
children:[
Text(product[i].name),
MaterialButton(
child:Text('edit'),
onPressed:(){
Hive.box('products').putAt(i,product);
}
),
]
),
}}
Try .save() method:
_personBox = Hive.openBox('personBox');
await _personBox.then((item) {
if (!item.isEmpty) {
print('empty');
var i = item.getAt(0, PersonModel(...,..,..,..,...,..));
i.profile = Somevalue;
i.save();
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With