【问题标题】:How to retrieve data column by column in dart from list of maps?如何从地图列表中逐列检索飞镖中的数据?
【发布时间】:2019-08-29 10:42:56
【问题描述】:

我需要从 SQLite 数据库的 2 列中检索值,并且我正在使用辅助查询对其进行查询。

我尝试通过索引值。

print("In the login List : "+ lg[0].toString());

但我得到了

In the login List : {email: xxxx@gmail.com, password: xxxx}

  Future<bool> loginUser(String username, String password) async {
    bool isSuccess;
    Database db = await this.database;
    List<Map<String, dynamic>> lg = await db.query(regTable,
        columns: [colEmail, colPassword],
        where: "$colEmail = ? AND $colPassword = ?",
        whereArgs: [username, password]);
    if (lg.length > 0) {
      print("In the login List : "+ lg[0].toString());
      isSuccess = true;
    } else {
      isSuccess = false;
    }
    return isSuccess;
  }

我需要能够分别检索电子邮件和密码。 喜欢, print(Email : lg(email));

【问题讨论】:

    标签: database list sqlite dart flutter


    【解决方案1】:

    我想我已经明白了。

          lg.forEach((f){
            print(f["password"]);
          });
    

    这让我得到了我想要的。但如果您有任何建议,请随时提出。

    【讨论】:

      【解决方案2】:

      您有一个 ListMaps。看起来您想从该列表中生成特定键值的列表。 List 方法 map 非常适合此类问题。例如:

      final emails = lg.map((item) => item["email"]), 
        passwords = lg.map((item) => item["password"]);
      

      (这些实际上是普通的Iterables;如果你想要Lists,只需在末尾添加.toList()。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-06
        • 2020-10-16
        相关资源
        最近更新 更多