last day (15 days later) » 

07:17
0
A: Retrieve data in Hive

Sagar Sablein your case index is not enough you need to pass the index key var propertyEntity = await propertiesBox.get(list[0].key); //list[0] is your index of element so pass key of that element @override Future<PropertiesEntity?> getPropertyDetails() async { try { final propertiesBox...

But what if my list[0] is not entity domain?
try like this if that solves you issue var propertyEntity = await propertiesBox.get(list<propertyEntity>[0].key); or just get(0);
answer updated please check
E/flutter (14165): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: NoSuchMethodError: The method 'compareTo' was called on null. E/flutter (14165): Receiver: null E/flutter (14165): Tried calling: compareTo("list")
This error pointed to this line var propertyEntity = await propertiesBox.get(list[index].key);
does your getPropertiesList() function working properly? because i am using your code only
07:18
hey
what result you get after calling getPropertiesList()
@override
Future<PropertiesEntity?> getPropertyDetails(int index) async {
try {
final propertiesBox = await Hive.openLazyBox(_propertyBox);
var list = await propertiesBox.get('list');
list.cast<PropertiesEntity>();
debugPrint(list.toString());
debugPrint(list[0].key.toString());
var propertyEntity = await propertiesBox.get(list[0].key);

debugPrint(propertyEntity.id.toString());
return null;
} on CacheException catch (e) {
throw CacheException(e.message);
}
}
I/flutter (14165): [Instance of 'PropertiesEntity', Instance of 'PropertiesEntity']
I/flutter (14165): null
E/flutter (14165): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: NoSuchMethodError: The method 'compareTo' was called on null.
E/flutter (14165): Receiver: null
E/flutter (14165): Tried calling: compareTo("list")
the key is null
@override
Future<List<PropertiesEntity>?> getPropertiesList() async {
try {
final propertiesBox = await Hive.openLazyBox(_propertyBox);
if (propertiesBox.isEmpty) return [];

var list = await propertiesBox.get('list');

if (list == null) return [];

return list.cast<PropertiesEntity>();
} on CacheException catch (e) {
throw CacheException(e.message);
}
}
share result for this
var result = list.cast<PropertiesEntity>();
share result value
you here?
var result = list.cast<PropertiesEntity>();
debugPrint("Result " + result.toString());
Result [Instance of 'PropertiesEntity', Instance of 'PropertiesEntity']
what value you are getting from getPropertiesList(); in Future<List<PropertiesEntity>?>
because that value matters in getting index key
It returns list to me
is this correct?
07:24
can you share list here
share propertiesList without parsing
@override
Future<PropertiesEntity?> getPropertyDetails(int index) async {
try {
final propertiesBox = await Hive.openLazyBox(_propertyBox);
var list = await propertiesBox.get('list');
list = list.cast<PropertiesEntity>();
for (var i in list) {
debugPrint("id " + i.id.toString());
debugPrint("name " + i.name);
}
var propertyEntity = await propertiesBox.get(list[0].key);
debugPrint(propertyEntity.id.toString());
return null;
} on CacheException catch (e) {
throw CacheException(e.message);
}
}
I/flutter (14165): id 6
I/flutter (14165): name Trion KL
I/flutter (14165): id 7
I/flutter (14165): name Zoinla Home
E/flutter (14165): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: NoSuchMethodError: The method 'compareTo' was called on null.
E/flutter (14165): Receiver: null
E/flutter (14165): Tried calling: compareTo("list")
i think key is null
I storing list in this way

@override
Future<void> saveProperty(List? propertyEntity) async {
try {
final invoiceBox = await Hive.openLazyBox(_propertyBox);
await invoiceBox.put('list', propertyEntity);
} on CacheException catch (e) {
throw CacheException(e.message);
}
}
give me 2mins checking
can you share what is List? propertyEntity
i mean value inside propertyEntity inside
@override
Future<void> saveProperty(List? propertyEntity) async {
try {
final invoiceBox = await Hive.openLazyBox(_propertyBox);
await invoiceBox.put('list', propertyEntity);
} on CacheException catch (e) {
throw CacheException(e.message);
}
}
Meanwhile try this
@override
Future<PropertiesEntity?> getPropertyDetails(int index) async {
try {
final propertiesBox = await Hive.openLazyBox(_propertyBox);
var list = await propertiesBox.get('list');
list = list.cast<PropertiesEntity>();
for (var i in list) {
debugPrint("id " + i.id.toString());
debugPrint("name " + i.name);
}
var propertyEntity = await propertiesBox.get(list[0].key);
debugPrint(propertyEntity.toString());
return propertyEntity;
} on CacheException catch (e) {
throw CacheException(e.message);
Share this value debugPrint(propertyEntity.toString());
I/flutter (14165): id 6
I/flutter (14165): name Trion KL
I/flutter (14165): id 7
I/flutter (14165): name Zoinla Home
E/flutter (14165): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: NoSuchMethodError: The method 'compareTo' was called on null.
E/flutter (14165): Receiver: null
E/flutter (14165): Tried calling: compareTo("list")
I think it crash at var propertyEntity = await propertiesBox.get(list[0].key);
07:35
you want to save list inside HiveData
I did
@override
Future<void> saveProperty(List? propertyEntity) async {
try {
final invoiceBox = await Hive.openLazyBox(_propertyBox);
await invoiceBox.put('list', propertyEntity);
} on CacheException catch (e) {
throw CacheException(e.message);
}
}
}
do I need to add key in propertyEntity model?
@HiveType(typeId: 10)
class PropertiesEntity extends HiveObject {
@HiveField(0)
String name;

@HiveField(1)
String id;

PropertiesEntity({
required this.id,
required this.name,
});

Property toProperties() {
return Property(
id: id,
name: name);
}
}
here the entity
then it should be like this
final invoiceBox = await Hive.openLazyBox(_propertyBox);
Future<void> saveProperty((PropertiesEntity? value)=> invoiceBox.add(value)
should use add instead of put?
yes
you have model ready so use add
Future<void> saveProperty((PropertiesEntity? value)=> invoiceBox.add(value)

this line is used to store list?
07:40
yes
but why is PropertiesEntity? value
my param is list
its your data list
yes
if you want to add data then use this fucntio like
saveProperty(propertyList);
make this fucntion global somewhere in a class
Future<void> saveProperty((PropertiesEntity? value)=> invoiceBox.add(value)

then use it from anywhere using
saveProperty(propertyList);
@override
Future<void> saveProperty(List? propertyEntity) async {
try {
final invoiceBox = await Hive.openLazyBox(_propertyBox);
await invoiceBox.put('list', propertyEntity);
} on CacheException catch (e) {
throw CacheException(e.message);
}
}
}
also for getting data back
and to get value final invoiceBox = await Hive.openLazyBox(_propertyBox);
create this global fucntion
List<PropertiesEntity?> getPropertyEntity() => invoiceBox.keys.map((key) {
PropertiesEntity value = invoiceBox.get(key);
});.toList();
how should U change this line?
07:44
just add
await invoiceBox.add(propertyEntity);
replace this?
await invoiceBox.put('list', propertyEntity);
yes
you have adapter class for class PropertiesEntity which will take care of it
when we have adapter no need to mentione key
let me try
ok try and ping me else i will create and share class
@override
Future<List<PropertiesEntity>?> getPropertiesList() async {
try {
final propertiesBox = await Hive.openLazyBox(_propertyBox);
if (propertiesBox.isEmpty) return [];

var list = propertiesBox.keys.map((key) {
PropertiesEntity value = propertiesBox.get(key) as PropertiesEntity;
});

// var list = await propertiesBox.get('list');

if (list == null) return [];
return list.cast<PropertiesEntity>();
} on CacheException catch (e) {
throw CacheException(e.message);
}
}
like this?
A value of type 'Iterable<PropertiesEntity>' can't be returned from the method 'getPropertiesList' because it has a return type of 'Future<List<PropertiesEntity>?>'.
Edited
@override
Future<List<PropertiesEntity>?> getPropertiesList() async {
try {
final propertiesBox = await Hive.openLazyBox(_propertyBox);
if (propertiesBox.isEmpty) return [];

var list = propertiesBox.keys.map((key) {
PropertiesEntity value = propertiesBox.get(key) as PropertiesEntity;
}).toList();

// var list = await propertiesBox.get('list');

if (list == null) return [];
return list.cast<PropertiesEntity>();
} on CacheException catch (e) {
throw CacheException(e.message);
}
}
@SagarSable
07:56
give me 5mins
will create classes and share with you
Hey
can you run this line only
var list = propertiesBox.keys.map((key) {
PropertiesEntity value = propertiesBox.get(key) as PropertiesEntity;
});
out of this fuction inside init
init(){
var list = propertiesBox.keys.map((key) {
PropertiesEntity value = propertiesBox.get(key) as PropertiesEntity;
});
print(list);
}
you are mixing 2 things
can put like this?
@override
Future<List<PropertiesEntity>?> getPropertiesList() async {
try {
final propertiesBox = await Hive.openLazyBox(_propertyBox);
if (propertiesBox.isEmpty) return [];

var list = propertiesBox.keys.map((key) {
PropertiesEntity value = propertiesBox.get(key) as PropertiesEntity;
});
print(list);
} on CacheException catch (e) {
throw CacheException(e.message);
}
}
Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'PropertiesEntity' in type cast
Create a new Classs
.
.
.
import 'package:hive_flutter/hive_flutter.dart';

class HiveData {
static final _box = Hive.box('propertyEntity');

static List<PropertyEntity> getProperty() =>
_box.keys.map((key) {
PropertyEntity value = _box.get(key);
return value;
}).toList();

static Future<void> saveProperty(PropertyEntity value) =>
_box.add(value);
}
like this
and insted of calling getPropertiesList(); call HiveData.getProperty(); from above call
and to save
saveProperty(propertyEntity);
make functions golbal bro what you are doing is wrong approach
but what if i want to save a list?
static Future<void> saveProperty(PropertyEntity value) =>
_box.add(value);
}
this just save an item?
using a forEach loop will work just fine
can i just make it inside a function?
I want to add them in my implementation file
@override
Future<List<PropertiesEntity>?> getPropertiesList() async {
try {
final propertiesBox = await Hive.openLazyBox(_propertyBox);
if (propertiesBox.isEmpty) return [];

var list = propertiesBox.keys.map((key) {
var value = propertiesBox.get(key);
return value;
});
return list;
} on CacheException catch (e) {
throw CacheException(e.message);
}
}

like this can?
08:11
yes
try and make some changes it will work
but i get this error in the line return list
A value of type 'Iterable<Future<dynamic>>' can't be returned from the method 'getPropertiesList' because it has a return type of 'Future<List<PropertiesEntity>?>'
08:23
change return lsit; to return list.cast<PropertiesEntity>();
let me know the result
@override
Future<List<PropertiesEntity>?> getPropertiesList() async {
try {
final propertiesBox = await Hive.openLazyBox(_propertyBox);
if (propertiesBox.isEmpty) return [];

var list = propertiesBox.keys.map((key) {
var value = propertiesBox.get(key);
return value;
}).toList();
return list.cast<PropertiesEntity>();
} on CacheException catch (e) {
throw CacheException(e.message);
}
}
.
.
.
if this dosnt work then change
final propertiesBox = await Hive.openBox(_propertyBox);
08:49
hey can you try above code
ok..trying
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'PropertiesEntity' in type cast
final propertiesBox = await Hive.openBox(_propertyBox);
now add this and make flutter clean
and reinstall your app
do i need add async and await?
yes some some functions its required
09:01
I am trying to replicate your code
thanks you'
This is my latest code
var propertiesList = await _propertiesLocalDataSource.getPropertiesList();
debugPrint(propertiesList.toString());
LocalDataSource

@override
Future<List<PropertiesEntity>?> getPropertiesList() async {
try {
final propertiesBox = await Hive.openLazyBox(_propertyBox);
if (propertiesBox.isEmpty) return [];

var list = propertiesBox.keys.map((key) async {
var value = await propertiesBox.get(key);

debugPrint("ggr " + value.toString());
return await value;
}).toList();

return list.cast<PropertiesEntity>();
} on CacheException catch (e) {
throw CacheException(e.message);
}
}
share PropertiesEntity class
@HiveType(typeId: 10)
class PropertiesEntity extends HiveObject {
@HiveField(0)
String name;

@HiveField(1)
String id;

PropertiesEntity({
required this.id,
required this.name,
});

Property toProperties() {
return Property(
id: id,
name: name);
}
}
09:29
just keep this tab running need 10min more
no problem
i just want store as list, able retrieve as list, and retrieve item based on specific id..that's it
10:15
here ? @SagarSable
yes
i am getting erorr solving that
you get same error with mine?
list is getting generated as lenght of list is proper
only list is iterable
so have to separate it
10:28
how?
trying that
if you want check list.length using print
it will show you items inside list
use getAt?
no use print(list.length); in getPropertyList
ok
but how does this solve my issue
it means data is there we just have to check for proper format
10:39
wonder why getAt(0) will return 2 items
i guess i got it
@override
Future<List<PropertiesEntity>?> getPropertiesList() async {
try {
final propertiesBox = await Hive.openBox("data");
var list = propertiesBox.getAt(0);
for(var i in list){
print(i.name);
}
return list;
} catch (e) {
print("data==========$e");
}
}
@override
Future<void> saveProperty(List<PropertiesEntity> propertyEntity) async {
try {
final invoiceBox = await Hive.openBox("data");
await invoiceBox.put("hi", propertyEntity);
} catch (e) {
print(e.toString());
}
}
}
check with these fucntions
change your variables
saveProperty still same with my old code
@override
Future<List<PropertiesEntity>?> getPropertiesList() async {
try {
final propertiesBox = await Hive.openBox(_propertyBox);
var data = propertiesBox.getAt(0);
for(var i in data){
print(i.name);
}
return data;
} catch (e) {
print("data==========$e");
}
}

saveProperty(List<PropertiesEntity> propertyEntity) async {
try {
final invoiceBox = await Hive.openBox(_propertyBox);
await invoiceBox.put("propertyList", propertyEntity);
} catch (e) {
print(e.toString());
}
}
}
i have changed with your variable
you said we shoudl go by your code right
thats why i did not change your code
10:47
ok
wait
ok sure try it out
any different btw openLazyBox and openbox?
in save method, i use openLazyBox
if getList,i change to openBox
data==========HiveError: The box "propertybox" is already open and of type LazyBox<dynamic>.
openLazyBox can potentially open a Box twice
never user openlazyBox
make flutter clean
10:50
ok
uninstall you app and reinstall again
need to clear old data
got the name now
Trion KL
I/flutter (14044): Zoinla Home
I/flutter (14044): data==========type 'List<dynamic>' is not a subtype of type 'FutureOr<List<PropertiesEntity>?>'
data==========type 'List<dynamic>' is not a subtype of type 'FutureOr<List<PropertiesEntity>?>'
where this erro?
in getlist
i think data not same return datatype?
maybe your data issue
check all data properly
10:54
return data.cast<PropertiesEntity>();
it works now
as for me this code is working properly
Great Man
now need to retrieve specific item
COngratulations
Happy to help
not yet fixed ..my first issue haven't fixed
first issue?
what is first issue?
10:58
want retrive specific item
if you need to retrieve specific item pass index of that item to getPropertiesList
var data = propertiesBox.getAt(0); change 0 with idex
@override
Future<PropertiesEntity?> getPropertyDetails(int index) async {
try {
final propertiesBox = await Hive.openBox(_propertyBox);
var list = await propertiesBox.get('propertyList');
list = await list.cast<PropertiesEntity>();
var propertyEntity = await propertiesBox.getAt(0);
debugPrint("fefef " + propertyEntity.toString());
return await propertyEntity;
} on CacheException catch (e) {
throw CacheException(e.message);
}
}
I don't know why it will return 2 items
output
fefef [Instance of 'PropertiesEntity', Instance of 'PropertiesEntity']
it should only return one right
@override
Future<PropertiesEntity?> getPropertyDetails(int index) async {
try {
final propertiesBox = await Hive.openBox(_propertyBox);

var propertyEntity = await propertiesBox.getAt(index);
propertyEntity = await propertyEntity.cast<PropertiesEntity>();
return propertyEntity;
} on CacheException catch (e) {
throw CacheException(e.message);
}
}
try this
same
11:06
thanks
Its done
@override
Future<PropertiesEntity?> getPropertyDetails(int index) async {
try {
final propertiesBox = await Hive.openBox("data");
List<PropertiesEntity>? allData =propertiesBox.get("propertyList");
print(allData!.length);
var propertyEntity = allData.elementAt(index);
print(propertyEntity.name);
return propertyEntity;
} catch (e) {
print("data==========$e");
}
}
as you are not using list element you wont get index.key value
if you have access to list of PropertyEntity
then use like this getPropertyDetails(index.key);
is it working?
11:22
wait..i checking
it works !
thanks
let me update anser
and accept it pls
for the getlist method, I saw you using
var data = propertiesBox.getAt(0);
why need use getAt? If I use back my own code, also same?
you can change code to
@override
Future<List<PropertiesEntity>?> getPropertiesList() async {
try {
final propertiesBox = await Hive.openBox("data");
List<PropertiesEntity>? allData =propertiesBox.get("propertyList");
for(var j in allData!){
print(j.name);
}
return allData;
} catch (e) {
print("data==========$e");
}
}
this will give you all list
ok,thanks again
please accept anser
and upvote it
you here?
john can you please accept answer and upvote it, It will be worth my time spend here bro
11:34
sure i will
you helped a lot
appreciate
Thank you so Much
Means a lot
actually I dont understand why getAt not working
need use elementAt instead
you need to have key for that
as you dont have reference of item list which gives access to key
because i using put?
no
you need data from model before fetching the data which is not possible here
once you have list of all elements from ther you get keys
before that its not possible
mainly getAt is used when your operations are offline only
11:38
i see
all the references should be from local not from server as server index wont have key
only local index from model class will have keys stored as unique identifier
but now i want fetch from local
after save list from server to local
you can do that
get all list
List<PropertiesEntity>? allData =propertiesBox.get("propertyList");
then
var propertyEntity = await propertiesBox.getAt(allData[index].key);
this is how you need to use getAt()
I have few Calls now
time to code bro
if you have anyqueries
do let me know
no problem
thanks for your helped
all the best to you
Welcome Have a great day
11:57
@SagarSable If I want to delete,write like this?
@override
Future<void> deletePropertyBasedOnId(int key) async {
final propertiesBox = await Hive.openBox(_propertyBox);
propertiesBox.delete(key);
}
the key is the listview index?
12:14
yes just pass index like deletePropertyBasedOnId(index.key)
key is unique identifier if every element index

last day (15 days later) »