Getting the other values in a record if given the ID in Isar database using Flutter

370 views Asked by At

I'm using the Isar database in a Flutter application, but I'm unable access the properties of a stored object. I'm only able to retrieve an Instance of the Object.

This is my collection:

@Collection()
 class User{
 Id? id;
 late String name;
 late String email;
 
 @Index()
 late String phone;
}

This is how I'm trying to access the record from the login page:

final users = widget.isar.users.where() .phoneEqualTo(phone) .findAll();

In this case users is returned as [Instance of 'User']

I want to access the name of the record corresponding to the phone number. How would I go about this? Thanks in advance!

1

There are 1 answers

0
WebDesk Solution On

@Sharvan Kumaran Simply get the initial element and convert it into a User object.

final users = widget.isar.users.where().phoneEqualTo(phone).findAll();

if (users.isNotEmpty) {
  User user = users.first;
  String name = user.name;
  String email = user.email;
}