HDiamond
HDiamond

Reputation: 1097

Grabbing Primary Key of listed values from sqlite database, dart with flutter

Currently my code is grabbing the order of the listed values by how it is indexed and i'm passing it to my raw sql query. I think i may be going about this the wrong way buy using listviewbuilder, but I want to grab the primary key (from sqlite database) of the clicked on value and pass that to the query on the next page

      body: !loading ? new ListView.builder(
          itemCount: sectorList.length,
          itemBuilder: (BuildContext context, int index) {
            return new Card(
              color: Colors.cyan[800],
              elevation: 2.0,
              child: new ListTile(
                  title: new Text("${sectorList[index]}"),
                  onTap: () {
                    Navigator.push(context,
                      MaterialPageRoute(
                        builder: (context) =>
                            DocumentPage(id: index),
                      ),
                    );
                  }),
            );
          },
      ) : CircularProgressIndicator(),
    );
  }
}

any help or guidance would be greatly appreciated. Comment if you need to see more of the code.

Upvotes: 0

Views: 1156

Answers (1)

Richard Heap
Richard Heap

Reputation: 51682

You must be using some type of query like:

List<Map<String, dynamic>> categories =
  await db.rawQuery('SELECT * FROM tbl_categories order by category_name');

This is going to give you a List with each of the categories ordered by name. If the table also contains another column (maybe category_id) that will also be available in the map.

Now each ListTile looks like:

          child: new ListTile(
              title: new Text("${categories[index]['category_name']}"),
              onTap: () {
                Navigator.push(context,
                  MaterialPageRoute(
                    builder: (context) =>
                        OtherPage(id: categories[index]['category_id']),
                  ),
                );
              },
            ),

You display the name, but pass the id to the next page. Assuming then that there's another table with a foreign key of category_id you could:

await db.rawQuery('SELECT * FROM tbl_other WHERE category_id=${widget.id}');

This would give you another List<Map<String, dynamic>> of all the other things in the relevant category. Use this to build this next page.

Upvotes: 1

Related Questions