扑动:sqflite 插入 return 类型

flutter: sqflite insert return type

与 sqflite 中的每个数据库操作一样,插入操作也将是一个 异步函数,它将 return 一个 int 类型的 Future,作为 insert 方法将 return 插入的记录的 ID,但在我的例子中它不会 return 记录它的 ID return 像这样 (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>'

insert_into_AccountreturnFuture<int>。获取id,在前面加上await

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