I’m using Supabase for data storage in my store application, which I’m developing with Dart (Flutter). In my database, I have two tables named product and product_image, where the product_image table has a many-to-one relationship with the product table. I want to retrieve the product information in the code, and this product requires images, which might include multiple images. My problem is that I don’t know how to fetch these products. I’m a junior developer and a bit confused, so please guide me.
ProductImage:
<code>class ProductImage {
final int id;
final DateTime createAt;
final int productId;
final String imageUrl;
final int displayOrder;
ProductImage({
required this.id,
required this.createAt,
required this.productId,
required this.imageUrl,
required this.displayOrder,
});
ProductImage.fromJson(Map<String, dynamic> json)
: id = json['id'],
createAt = DateTime.parse(json['created_at']),
productId = json['product_id'],
imageUrl = json['image'],
displayOrder = json['display_order'];
}
</code>
<code>class ProductImage {
final int id;
final DateTime createAt;
final int productId;
final String imageUrl;
final int displayOrder;
ProductImage({
required this.id,
required this.createAt,
required this.productId,
required this.imageUrl,
required this.displayOrder,
});
ProductImage.fromJson(Map<String, dynamic> json)
: id = json['id'],
createAt = DateTime.parse(json['created_at']),
productId = json['product_id'],
imageUrl = json['image'],
displayOrder = json['display_order'];
}
</code>
class ProductImage {
final int id;
final DateTime createAt;
final int productId;
final String imageUrl;
final int displayOrder;
ProductImage({
required this.id,
required this.createAt,
required this.productId,
required this.imageUrl,
required this.displayOrder,
});
ProductImage.fromJson(Map<String, dynamic> json)
: id = json['id'],
createAt = DateTime.parse(json['created_at']),
productId = json['product_id'],
imageUrl = json['image'],
displayOrder = json['display_order'];
}
Product
<code>class ProductSup {
final int id;
final DateTime createAt;
final String title;
final int price;
final int? previousPrice;
final List<int> productImage;
ProductSup(
{required this.id,
required this.createAt,
required this.title,
required this.price,
required this.previousPrice,
required this.productImage});
ProductSup.fromJson(Map<String, dynamic> json)
: id = json['id'],
createAt = DateTime.parse(json['created_at']),
title = json['title'],
price = json['price'],
previousPrice = json['previous_price'],
productImage = [];
}
</code>
<code>class ProductSup {
final int id;
final DateTime createAt;
final String title;
final int price;
final int? previousPrice;
final List<int> productImage;
ProductSup(
{required this.id,
required this.createAt,
required this.title,
required this.price,
required this.previousPrice,
required this.productImage});
ProductSup.fromJson(Map<String, dynamic> json)
: id = json['id'],
createAt = DateTime.parse(json['created_at']),
title = json['title'],
price = json['price'],
previousPrice = json['previous_price'],
productImage = [];
}
</code>
class ProductSup {
final int id;
final DateTime createAt;
final String title;
final int price;
final int? previousPrice;
final List<int> productImage;
ProductSup(
{required this.id,
required this.createAt,
required this.title,
required this.price,
required this.previousPrice,
required this.productImage});
ProductSup.fromJson(Map<String, dynamic> json)
: id = json['id'],
createAt = DateTime.parse(json['created_at']),
title = json['title'],
price = json['price'],
previousPrice = json['previous_price'],
productImage = [];
}
product table:
product_image: