Fix produk hilang: _loadProducts try-catch + auto refreshSession sebelum refresh
This commit is contained in:
@@ -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 {
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user