0

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:

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

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 table

product_image:

product_image

1 Answer 1

0

Your query should look something like this ->

// Have the [supabse] client prepared first!
final data = await supabase.from('product').select('id, title, price, product_image(id, image, display_order)').eq('id', '**YOUR_PRODUCT_ID**');

For more information, follow up this docs:

  1. One to may relationships in Supabase.
  2. Flutter docs for Supabase.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.