Fix produk hilang: _loadProducts try-catch + auto refreshSession sebelum refresh

This commit is contained in:
Onie
2026-10-18 12:15:28 +07:00
parent bbdb7b7b20
commit 2b7872ddbb

View File

@@ -191,6 +191,7 @@ class AppState extends ChangeNotifier {
}
Future<void> refresh() async {
await _ensureSession();
await _loadProducts();
await _loadSales();
notifyListeners();
@@ -505,12 +506,31 @@ class AppState extends ChangeNotifier {
}
Future<void> _loadProducts() async {
final rows = await _withTimeout(
_supabase.from('products').select().eq('store_id', _currentStoreId).order('name'),
'Load products',
);
products = rows.map((e) => Product.fromJson(e)).toList();
if (products.isEmpty) _seed();
try {
final rows = await _withTimeout(
_supabase.from('products').select().eq('store_id', _currentStoreId).order('name'),
'Load products',
);
products = rows.map((e) => Product.fromJson(e)).toList();
if (products.isEmpty) _seed();
} catch (e) {
// Jangan crash refresh: produk lama tetap ditampilkan, error tercatat di log.
debugPrint('Gagal load products: $e');
}
}
/// Coba perbarui session auth yang kedaluwarsa. Return true jika session valid.
Future<bool> _ensureSession() async {
try {
final session = _supabase.auth.currentSession;
if (session == null) return false;
if (!session.isExpired) return true;
final res = await _supabase.auth.refreshSession();
return res.session != null && !res.session!.isExpired;
} catch (e) {
debugPrint('Gagal refresh session: $e');
return false;
}
}
Future<void> _loadSales() async {