简体   繁体   English

如何在 flutter 中使用本地数据库填充 DropdownSearch flutter

[英]How to populate DropdownSearch flutter with local database in flutter

I want to populate my DropdownSearch with fetching data from local database.我想用从本地数据库获取数据来填充我的DropdownSearch Plese help me.请帮助我。

Future<dynamic> getList() async {
  final db = await getDatabase();
  final res = await db.rawQuery("SELECT * FROM tb_point_of_Sale");

  List<dynamic> list = 
      res.isNotEmpty ? res.map((c) => PointOfSale.fromJson(c)).toList() : [];
  }
body: Column(
  children: [
    DropdownSearch<String>(
      mode: Mode.MENU,
      items: PointOfSaleDao.db.getList(),
      showSearchBox: true,
      label: "Menu mode",
      hint: "point of sale in menu mode",
      onChanged: (value) {}
    ),
  ],
),

As getList returns a Future , use a FutureBuilder to draw the widget for each state the Future can be, whether it's still loading or finished fetching the data.getList返回Future时,使用FutureBuilderFuture可能处于的每个状态绘制小部件,无论它仍在加载还是已完成获取数据。

In case you are using the dropdown search package , there seems to be a field for that named asyncItems on it you might want to look into.如果您使用的是下拉搜索包,似乎有一个名为asyncItems的字段,您可能想要查看它。

Future getList() async {
final db = await getDatabase();
final res = await db.rawQuery("SELECT * FROM tb_point_of_Sale");

List<PointOfSale> list =
    res.isNotEmpty ? res.map((c) => PointOfSale.fromJson(c)).toList() : [];
return list;

} }

I used FutureBuilder but I don't know how to extract items data我使用了FutureBuilder但我不知道如何提取项目数据

          FutureBuilder(
              future: PointOfSaleDao.db.getAll(),
              builder: (BuildContext context, AsyncSnapshot snapshot) {
                if (!snapshot.hasData) {
                  return const Center(
                    child: CircularProgressIndicator(),
                  );
                } else {
                  return DropdownSearch<String>(
                      mode: Mode.MENU,
                      showSearchBox: true,
                      showSelectedItem: true,
                      items: snapshot
                          .data[0].id, //PointOfSaleDao.db.getList(),
                      onChanged: print,
                      hint: "sélectionner",
                      validator: (value) {
                        if (value == null) {
                          return "ce champ est obligatoire !";
                        }
                        return null;
                      });
                }
              },
            )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM