i am trying to add the product to initial state so i can basically show it on the screen.
class _ProductListState extends State
{
var dbHelper = DbHelper();
late List<Product> products;
int productCount = 0;
@override
void initState(){
var productsFuture = dbHelper.getProducts();
productsFuture.then((data){
this.products.add(data);
});
from dbHelper:
Future<List> getProducts() async
{
Database db = await this.db;
var result = await db.query("products");
return List.generate(result.length, (i)
{
return Product.fromObject(result[i]);
});
}
but i get an error. what should i do?
previously, code was like this:
class _ProductListState extends State
{
var dbHelper = DbHelper();
late List<Product> products;
int productCount = 0;
@override
void initState(){
var productsFuture = dbHelper.getProducts();
productsFuture.then((data){
this.products = data;
});
}
CodePudding user response:
try to provide list data type
Future<List<Product>> getProducts() async{