fix: enhance response parsing in ImportRepository to handle JSON array and improve logging
This commit is contained in:
@@ -71,18 +71,25 @@ class ImportRepository {
|
||||
}
|
||||
|
||||
final parsed = _parseResponse(response);
|
||||
developer.log('Response parsed, keys: ${parsed is Map ? parsed.keys.toList() : "list"}', name: 'ImportRepository');
|
||||
developer.log('Response parsed, keys: ${parsed is Map ? parsed.keys.toList() : "list[${(parsed as List?)?.length}]"}', name: 'ImportRepository');
|
||||
|
||||
// Backend returns ParsedReceiptItem[] directly as a JSON array
|
||||
if (parsed is List) {
|
||||
final items = parsed.map((e) => ParsedReceiptItem.fromJson(e as Map<String, dynamic>)).toList();
|
||||
developer.log('Successfully parsed ${items.length} items from array', name: 'ImportRepository');
|
||||
return items;
|
||||
}
|
||||
|
||||
if (parsed is Map<String, dynamic>) {
|
||||
// Structured receipt items from /receipt-import
|
||||
// Wrapped format: { items: [...] }
|
||||
if (parsed.containsKey('items')) {
|
||||
final items = (parsed['items'] as List?)?.map((e) => ParsedReceiptItem.fromJson(e)).toList();
|
||||
final items = (parsed['items'] as List?)?.map((e) => ParsedReceiptItem.fromJson(e as Map<String, dynamic>)).toList();
|
||||
if (items != null) {
|
||||
developer.log('Successfully parsed ${items.length} items', name: 'ImportRepository');
|
||||
return items;
|
||||
}
|
||||
}
|
||||
// Markdown-based receipt from /quick-import fallback — parse lines into items
|
||||
// Markdown-based receipt fallback — parse lines into items
|
||||
if (parsed.containsKey('markdown')) {
|
||||
developer.log('Got markdown receipt, parsing lines into items', name: 'ImportRepository');
|
||||
final lines = (parsed['markdown'] as String).split('\n');
|
||||
|
||||
Reference in New Issue
Block a user