Search code examples
flutterdartandroid-sqliteflutter-moor

get all records with same column value in an sqlite db table using moor_flutter package


I have a table called contacts. I want to get all records where person_id = 1 in contacts table below.

class Contacts extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get contact_type => text().withLength(min: 1, max: 50)();
  TextColumn get contact => text().withLength(min: 1, max: 100)();
  IntColumn get person_id =>
      integer().customConstraint('REFERENCES persons(id)')();
  DateTimeColumn get created => dateTime()();
  DateTimeColumn get updated => dateTime()();
}

How do I perform select * from contacts where person_id = 1 using moor_flutter? thank you, posted with Love.


Solution

  • You should have a look to the docs.

    You are looking for something like this :

    @UseMoor(tables: [Contacts])
    class MyDatabase extends _$MyDatabase {  
    
        Future<List<Contact>> getContactByPersonId(int id) {
            return (select(contacts)..where((c) => c.person_id.equals(id))).get();
        }
    }