Skip to content

Unable to retrieve data from my supabase database #1128

Open
@Cleevemadjouh

Description

@Cleevemadjouh

Hello, on my application the goal is to create a product, add the name, description, price, city and say if it is a new product or not. Then publish the product. The product arrives in my database but I can not retrieve the product and its information to put them on my home page.

Here is a set of screenshots of my database and error:

A Dart VM Service on iPhone 14 Pro is available at:
http://127.0.0.1:65219/9mPpn-pzT6s=/
The Flutter DevTools debugger and profiler on iPhone 14 Pro is available at:
http://127.0.0.1:9101?uri=http://127.0.0.1:65219/9mPpn-pzT6s=/
flutter: \^[[38;5;12m┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────<…>
flutter: \^[[38;5;12m│ #0   _AddProductPageState._publishProduct (package:ecommerceapp/screens/add_product_page.dart:316:12)<…>
flutter: \^[[38;5;12m│ #1   <asynchronous suspension><…>
flutter: \^[[38;5;12m├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄<…>
flutter: \^[[38;5;12m│ 💡 Image paths: [https://dkysfmevzveidazbbkqh.supabase.co/storage/v1/object/public/images-produits/products/1741339634024_0.jpg]<…>
flutter: \^[[38;5;12m└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────<…>
flutter: \^[[38;5;12m┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────<…>
flutter: \^[[38;5;12m│ #0   _AddProductPageState._publishProduct (package:ecommerceapp/screens/add_product_page.dart:354:20)<…>
flutter: \^[[38;5;12m│ #1   <asynchronous suspension><…>
flutter: \^[[38;5;12m├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄<…>
flutter: \^[[38;5;12m│ 💡 Fichier supprimé : /Users/cleevemadjouh/Library/Developer/CoreSimulator/Devices/FC198355-DA86-4C14-8EB5-CC0C8C0FFCD8/data/Containers/Data/Application/213D5042-2E9E-44EE-A8A5-AD6FC8AF5E83/tmp/image_picker_3DD7E2CF-D538-483E-AB31-96B8AB01DB2B-23255-0000047178DFABA7.jpg_compressed.jpg<…>
flutter: \^[[38;5;12m└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────<…>
flutter: \^[[38;5;196m┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────<…>
flutter: \^[[38;5;196m│ FormatException: Unexpected end of input (at character 1)<…>
flutter: \^[[38;5;196m│ <…>
flutter: \^[[38;5;196m│ ^<…>
flutter: \^[[38;5;196m│ <…>
flutter: \^[[38;5;196m├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄<…>
flutter: \^[[38;5;196m│ #0   HomePageState._fetchProducts (package:ecommerceapp/screens/home_page.dart:52:14)<…>
flutter: \^[[38;5;196m│ #1   <asynchronous suspension><…>
flutter: \^[[38;5;196m├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄<…>
flutter: \^[[38;5;196m│ ⛔ Erreur lors du chargement des produits : <…>
flutter: \^[[38;5;196m└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────<…>

Application finished.
Image Image Image Image

My product.dar code :

import 'package:flutter/foundation.dart';

class Product {
  final int id;
  final String name;
  final double price;
  final String description;
  final String imageUrl;
  final String sellerId;
  final String city;
  final String? category;
  final String? condition;
  final List<String> imageUrls;
  final DateTime created_at;

  Product({
    required this.id,
    required this.name,
    required this.price,
    required this.description,
    required this.imageUrl,
    required this.sellerId,
    required this.city,
    this.category,
    this.condition,
    this.imageUrls = const [],
    required this.created_at,
  });

  factory Product.fromJson(Map<String, dynamic> json) {
    try {
      final id = json['id'] is int
          ? json['id']
          : int.parse(json['id'].toString());

      final dynamic priceValue = json['price'];
      double price;
      if (priceValue is int) {
        price = priceValue.toDouble();
      } else if (priceValue is double) {
        price = priceValue;
      } else if (priceValue is String) {
        price = double.parse(priceValue);
      } else {
        throw FormatException('Format de prix invalide');
      }

      List<String> imageUrls = [];
      if (json['imageUrls'] != null) {
        imageUrls = (json['imageUrls'] as List<dynamic>)
            .map((url) => url.toString())
            .toList();
      }

      DateTime created_at = json['created_at'] != null
    ? DateTime.parse(json['created_at'] as String) // Correction déjà faite
    : DateTime.now();

      return Product(
        id: id,
        name: json['name'] ?? '',
        price: price,
        description: json['description'] ?? '',
        imageUrl: json['imageUrl'] ?? '',
        sellerId: json['seller_id'] ?? '',
        city: json['city'] ?? '',
        category: json['category'],
        condition: json['condition'],
        imageUrls: imageUrls,
        created_at: created_at,
      );
    } catch (e) {
      if (kDebugMode) {
        print('Erreur lors de la création du produit depuis JSON: $e');
      }
      rethrow;
    }
  }

  Map<String, dynamic> toJson() {
    try {
      return {
        'id': id,
        'name': name,
        'price': price,
        'description': description,
        'imageUrl': imageUrl,
        'seller_id': sellerId,
        'city': city,
        'category': category,
        'condition': condition,
        'imageUrls': imageUrls,
        'created_at': created_at.toIso8601String(),
      };
    } catch (e) {
      if (kDebugMode) {
        print('Erreur lors de la conversion du produit en Map: $e');
      }
      rethrow;
    }
  }

  bool isValid() {
    try {
      return id > 0 &&
          name.isNotEmpty &&
          price >= 0 &&
          description.isNotEmpty &&
          city.isNotEmpty;
    } catch (e) {
      if (kDebugMode) {
        print('Erreur lors de la validation du produit: $e');
      }
      return false;
    }
  }

  @override
  String toString() {
    return 'Product{id: $id, name: $name, price: $price, description: $description, '
        'city: $city, category: $category, condition: $condition}';
  }

  Product copyWith({
    int? id,
    String? name,
    double? price,
    String? description,
    String? imageUrl,
    String? sellerId,
    String? city,
    String? category,
    String? condition,
    List<String>? imageUrls,
    DateTime? created_at,
  }) {
    return Product(
      id: id ?? this.id,
      name: name ?? this.name,
      price: price ?? this.price,
      description: description ?? this.description,
      imageUrl: imageUrl ?? this.imageUrl,
      sellerId: sellerId ?? this.sellerId,
      city: city ?? this.city,
      category: category ?? this.category,
      condition: condition ?? this.condition,
      imageUrls: imageUrls ?? this.imageUrls,
      created_at: created_at ?? this.created_at,
    );
  }
}

My fetch methode:

Future<void> _fetchProducts() async {
    
    try {
      productsFuture =
          ProductService().fetchProducts(); // Utilise ProductService
      final products = await productsFuture;
      setState(() {
        allProducts = products;
        displayedProducts = products;
      });

    } catch (e) {
      logger.e("Erreur lors du chargement des produits : ", error: e);
      // Affiche un message d'erreur à l'utilisateur
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text("Erreur lors du chargement des produits : $e"),
          backgroundColor: Colors.red,
        ),
      );
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpostgrestThis issue or pull request is related to postgrest

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions