Reputation: 103
I am trying to receive the return value of the future as a string. How do i go about it.
//Get a stock info
Future<String> getStock(int productID) async{
var dbClient = await db;
var result = await dbClient.rawQuery('SELECT * FROM $tableStock WHERE $columnProductID = $productID');
if(result.length == 0) return null;
return Stock.fromMap(result.first).currentStock;
}
Widget _buildProductInfo(Product data){
return Container(
child: ListView(
padding: EdgeInsets.all(8.0),
children: <Widget>[
_infoRow('Product ID', data.name),
_infoRow('Product Name', data.productID),
_infoRow('Cost Price', data.costPrice),
_infoRow('Selling Price', data.salePrice),
_infoRow('CategoryID', data.categoryID),
_infoRow('Currrent Stock', db.getStock(int.parse(data.productID)))
],
),
);
}
I expect this code to show a "value" rather it says "Instance of Future". But i can print the returned value when i try
final res = await db.getStock(int.parse(data.productID);
print(res);
Upvotes: 0
Views: 459
Reputation: 18593
You have to await for the future in order to unwrap the value. You can use a future builder to do this.
Instead of having this:
_infoRow('Currrent Stock', db.getStock(int.parse(data.productID))),
Have this:
FutureBuilder(
future: db.getStock(int.parse(data.productID),
builder: (context, snapshot) => _infoRow('Currrent Stock', snapshot.data),
),
Your complete code will look like this:
child: StreamBuilder<Product>(
initialData: barcode,
stream: bloc.scannedCode,
builder: (BuildContext context, AsyncSnapshot snapshot){
if (snapshot.hasError) return Text('Error: ${snapshot.error}');
switch (snapshot.connectionState) {
case ConnectionState.none:
return Text('Select lot');
case ConnectionState.waiting:
return _buildProductInfo(snapshot.data);
case ConnectionState.active:
case ConnectionState.done:
return _buildProductInfo(snapshot.data);
}
},
)
Widget _buildProductInfo(Product data){
return Container(
child: ListView(
padding: EdgeInsets.all(8.0),
children: <Widget>[
_infoRow('Product ID', data.name),
_infoRow('Product Name', data.productID),
_infoRow('Cost Price', data.costPrice),
_infoRow('Selling Price', data.salePrice),
_infoRow('CategoryID', data.categoryID),
FutureBuilder(
future: db.getStock(int.parse(data.productID),
builder: (context, snapshot) => _infoRow('Currrent Stock', snapshot.data),
)
],
),
);
}
Upvotes: 1
Reputation: 3305
You have to use async
on your _buildProductInfo()
method and use await
before db.getStock(int.parse(data.productID))
. This way, the execution is suspended until the Future completes.
Upvotes: 0