I have 3 dart classes:
class Stock {
final int? id;
final String? name;
final String? timestamp;
Stock({
this.id,
this.name,
this.timestamp,
});
factory Stock.fromMap(Map<String, dynamic> json) => Stock(
id: json['id'],
name: json['name'],
timestamp: json['timestamp'],
);
Map<String, dynamic> toMap() {
return {
'id' : id,
'name' : name,
'timestamp' : timestamp,
};
}
}
class Product {
final int? id;
final int? barcode;
final String? name;
final String? timestamp;
Product({
this.id,
this.barcode,
this.name,
this.timestamp,
});
factory Product.fromMap(Map<String, dynamic> json) => Product(
id: json['id'],
barcode: json['barcode'],
name: json['name'],
timestamp: json['timestamp'],
);
Map<String, dynamic> toMap() {
return {
'id' : id,
'barcode' : barcode,
'name' : name,
'timestamp' : timestamp,
};
}
}
import 'package:stock_manager/model/product.dart';
import 'package:stock_manager/model/stock.dart';
class StockProduct {
final int? id;
final Stock stock;
final Product product;
final int cnt;
final int del;
final String timestamp;
StockProduct({
this.id,
required this.stock,
required this.product,
required this.cnt,
required this.del,
required this.timestamp,
});
factory StockProduct.fromMap(Map<String, dynamic> json) => StockProduct(
id: json['id'],
stock: Stock.fromMap(json['stock_id']),
product: Product.fromMap(json['product_id']),
cnt: json['cnt'],
del: json['del'],
timestamp: json['timestamp'],
);
Map<String, dynamic> toMap() {
return {
'id' : id,
'stock_id' : stock.toMap(),
'product_id' : product.toMap(),
'cnt' : cnt,
'del' : del,
'timestamp' : timestamp,
};
}
}
And i have this database function:
Future<List<StockProduct>> getStockProducts() async {
Database db = await _databaseService.database;
var stockProdcuts = await db.query('stock_product');
List<StockProduct> stockProdcutList = stockProdcuts.isNotEmpty ? stockProdcuts.map((c) => StockProduct.fromMap(c)).toList() : [];
return stockProdcutList;
}
My problem is when i run getStockProducts() nothing is returned and i dont get an error. It stops at .toList().
stockProducts.length is returning 3 (i have 3 stock_products) in the database.
I expect the function to return 3 Stock Products in a List
New contributor
Askadi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.