从sqlite数据库中获取列出的值的主键,飞镖飞镖

时间:2018-07-19 21:15:18

标签: sqlite dart flutter

当前,我的代码通过如何索引索引值来列出所列值的顺序,并将其传递给我的原始sql查询。我认为我可能会使用listviewbuilder进行购买,但是我想从点击值的主键(从sqlite数据库中获取)并将其传递给下一页的查询

      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(),
    );
  }
}

任何帮助或指导将不胜感激。 如果需要查看更多代码,请发表评论。

1 个答案:

答案 0 :(得分:1)

您必须使用某种类型的查询,例如:

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

这将为您提供一个列表,其中包含按名称排序的每个类别。如果该表还包含另一列(也许是category_id),则该列也将在地图中可用。

现在每个ListTile如下:

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

您显示名称,但将ID传递到下一页。假设存在另一个带有category_id外键的表,您可以:

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

这将为您提供相关类别中所有其他事物的另外List<Map<String, dynamic>>。使用它来构建下一页。