databasefluttersqflitefloorjunction-table

Get Data from Two Tables Floor Flutter


I want to get data from two tables using floor in flutter, how can I do that?

this code for getting data from one table. this function inside database.g.dart:

Stream<List<Item>> getItems() {
    return _queryAdapter.queryListStream(
        'SELECT * FROM Item WHERE is_active = 1',
        mapper: (Map<String, Object?> row) => Item(
            id: row['id'] as int?,
            item_id: row['item_id'] as String,
            description: row['description'] as String,
            salePrice: row['sale_price'] as double,
            purchasePrice: row['purchase_price'] as double,
            isActive: row['is_active'] as int),
        queryableName: 'Item',
        isView: false);
  }

So the return will be Item objects but what if I have junction table from another tables and the command will be like this:

SELECT junctiont.*, item.*, client.*
FROM item
INNER JOIN junctiont
ON item.id = junctiont.item_id
INNER JOIN client
ON junctiont.client_id = client.id

How the function will be? and what will return?.


Solution

  • I found the answer by create new object to return the whole coming columns or you can return to list

    @override
      Stream<List<List>> getBillsMainInfo() {
        return _queryAdapter.queryListStream(
            'SELECT Bills.id, Biils.bill_number, Bills.total, Bills.datetime, Clients.name FROM Bills INNER JOIN Clients ON Bills.client_id = Clients.id WHERE Bills.is_active = 1',
            mapper: (Map<String, Object?> row) => [
              row['id'] as int,
              row['bill_number'] as int,
              row['datetime'] as String,
              row['name'] as String,
              row['total'] as double
            ],
            queryableName: 'Bills',
            isView: false);
      }