颤振:sqflite 插入返回类型

时间:2021-05-05 12:38:54

标签: sql sqlite flutter sqflite

与 sqflite 中的每个数据库操作一样,插入操作也将是一个 异步函数,它将返回一个 int 类型的 Future,作为 insert 方法将返回插入的记录的 ID,但在我的情况下,它不返回记录的 id,它返回类似 (Instance of 'Future<int>') 的内容。我想打印我记录的实际数值 id。

这是我插入数据的代码:

Future<int> insert_into_Account(Account account) async {


    int id = await db.insert(
      'account',
      account.toMapAccount(),
      conflictAlgorithm: ConflictAlgorithm.replace,
    );
    return id;
  }

  // print data
Future testDb() async {
    db = await openDb();


    var id = insert_into_Account(Account(
      name: 'maliha',
      adress: 'eeee',
      balance: 123,
      issueDate: '333',
      telphone: 'ddd',
      type: 'eee',
    ));
    List lists = await db.rawQuery('select * from account');

    print(lists[0].toString());
    print(id.toString());
  }

结果是:

I/flutter (11306): {accountNumber: 5, name: maliha, telphone: ddd, address: eeee, issueDate: 333, balance: 123, image: null, type: eee}
I/flutter (11306): Instance of 'Future<int>'

1 个答案:

答案 0 :(得分:1)

insert_into_Account 返回 Future<int>。要获取 id,请在前面添加 await

 var id = await insert_into_Account(Account(
      name: 'maliha',
      adress: 'eeee',
      balance: 123,
      issueDate: '333',
      telphone: 'ddd',
      type: 'eee',
    ));