soo im trying to retrieve some data for food items that can be purchased in my app. Ive called the API and i have connected it to my Isar database for local storage but it is not displaying the items from the API after i added the price feature. Here is my API service file
import 'dart:convert';
import 'package:http/http.dart' as http;
import '../model/food_item.dart';
class ApiService {
final String apiKey = 'MY_API_KEY';
final String apiUrl = 'https://api.spoonacular.com/food/products/search';
Future<List<FoodItem>> fetchFoodItems({int number = 10}) async {
final response = await http.get(Uri.parse('$apiUrl?apiKey=$apiKey'));
if (response.statusCode == 200) {
final data = json.decode(response.body);
final List<dynamic> products = data['products'];
return products.map((product) {
return FoodItem(
id: product['id'],
title: product['title'],
image: product['image'],
price: (product['price'] as num?)?.toDouble() ?? 0.0,
);
}).toList();
} else {
throw Exception('Failed to load food items');
}
}
}
And here is my food_item.dart file
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:isar/isar.dart';
part 'food_item.freezed.dart';
part 'food_item.g.dart';
@freezed
class FoodItem with _$FoodItem {
@JsonSerializable(explicitToJson: true)
const factory FoodItem({
required int id,
required String title,
required String image,
required double price,
}) = _FoodItem;
factory FoodItem.fromJson(Map<String, dynamic> json) => _$FoodItemFromJson(json);
}
@collection
class FoodItemSchema {
Id? id;
late String title;
late String image;
late double price;
FoodItemSchema();
factory FoodItemSchema.fromFoodItem(FoodItem item) {
return FoodItemSchema()
..id = item.id
..title = item.title
..image = item.image
..price = item.price;
}
FoodItem toFoodItem() {
return FoodItem(
id: id!,
title: title,
image: image,
price: price,
);
}
}
And finally my food_Store.dart file
import 'package:isar/isar.dart';
import 'package:mobx/mobx.dart';
import '../model/food_item.dart';
import '../service/api_service.dart';
import '../service/isar_service.dart';
part 'food_store.g.dart';
class FoodStore = _FoodStore with _$FoodStore;
abstract class _FoodStore with Store {
final IsarService isarService = IsarService();
final ApiService apiService = ApiService();
@observable
ObservableList<FoodItem> foodItems = ObservableList<FoodItem>();
@observable
ObservableList<FoodItem> shoppingBasket = ObservableList<FoodItem>();
@action
Future<void> loadFoodItems() async {
final isar = await isarService.db;
final itemsFromDb = await isar.foodItemSchemas.where().findAll();
if (itemsFromDb.isEmpty) {
try {
final itemsFromApi = await apiService.fetchFoodItems();
foodItems.addAll(itemsFromApi);
await isarService.saveFoodItems(itemsFromApi);
} catch (e) {
print('Error fetching food items: $e');
}
} else {
foodItems.addAll(itemsFromDb.map((item) => item.toFoodItem()).toList());
}
}
@action
void addToBasket(FoodItem item) {
shoppingBasket.add(item);
}
@action
Future<void> checkout() async {
// Perform checkout logic here (e.g., POST request)
shoppingBasket.clear();
}
}
I think the thing causing it is within these three files.Your help will be much appreciated
I checked my API limits and found out they were still available so i ruled that out, i tried using build_runner to rebuild the code and it worked with no errors after adding the price. I went through the API service file but could still not find any errors from my point of view