flutter: sqflite insert return type
658
insert_into_Account
return Future<int>
. To get id, add await
in front:
var id = await insert_into_Account(Account(
name: 'maliha',
adress: 'eeee',
balance: 123,
issueDate: '333',
telphone: 'ddd',
type: 'eee',
));
Author by
maliha arash
Updated on December 29, 2022Comments
-
maliha arash over 1 year
As with every database operation in sqflite , insert operation also will be an asynchronous function, and it will return a Future of type int, as the insert method will return the ID of the record that was inserted, but in my case it dos not return id of record it return something like this
(Instance of 'Future<int>')
. I want to print the actual numeric value id of my record.This is my code for inserting data:
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()); }
and result is:
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>'