I build a simple flutter Food Ordering app, and I want to hardcode my Restaurants and products , it's a simple school project.
My problem is, I have a globals.dart file, where I keep some variables.
I have some restaurants in a List, some Products in a List, and some Keywords (Products have a list of Keywords) in a List.
My problem is that the list of products is null in the whole app. The restaurants List and the keywords list is working. It's driving me insane.
Restaurant pizza_hut = Restaurant("Pizza Hut", 4.9,
"https://i.ytimg.com/vi/CPs4owoNWlE/maxresdefault.jpg");
Restaurant salad_box = Restaurant("Salad Box", 4.2,
"https://portal.saladbox.ro//Uploads/Images/Location/0db90bc5-7674-4ffb-95b6-79072b0293cd.jpg");
Restaurant mcdonalds = Restaurant("McDonalds", 4.3,
"https://www.corbeancaonline.ro/wp-content/uploads/2018/04/mcdonalds-750x450.jpg");
List<Restaurant> restaurantList = [
pizza_hut,
salad_box,
mcdonalds
];
Product pizza_carnivora = Product("Pizza Carnivora", 23, pizza_hut , [keywords[6],keywords[12]]);
Product pizza_pepperoni = Product("Pizza Pepperoni" , 30 , pizza_hut , [keywords[6],keywords[12],]);
Product vegan_pizza = Product("Vegan Pizza", 20, pizza_hut, [keywords[6],keywords[12],keywords[15],]);
Product carbonara = Product("Pasta Carbonara", 20, pizza_hut, [keywords[4],keywords[14],keywords[9],]);
Product ceasar=Product("Ceasar Salad", 25, salad_box, [keywords[0],keywords[1],keywords[2],keywords[3],keywords[4],]);
Product vegan_salad=Product("Vegan Salad", 20, salad_box, [keywords[0],keywords[1],keywords[2],keywords[3],keywords[15],]);
Product chicken_wrap=Product("Chicken Wrap", 17, salad_box, [keywords[0],keywords[1],keywords[3],keywords[13],keywords[4],]);
Product big_mac=Product("Big Mac", 15, mcdonalds, [keywords[5],keywords[7],keywords[11],keywords[12]]);
Product chicken_nuggets=Product("Chicken Nuggets", 20, mcdonalds, [keywords[4],keywords[8],keywords[10],keywords[12],]);
Product mc_chicken=Product("McChicken" , 7 , mcdonalds , [keywords[4],keywords[7],keywords[11],keywords[12],]);
List<Product> productsList = [
pizza_carnivora,
pizza_pepperoni,
vegan_pizza,
carbonara,
ceasar,
vegan_salad,
chicken_wrap,
big_mac,
chicken_nuggets,
mc_chicken
];
List<String> keywords = [
"HEALTHY",//0
"HEALTY", //1
"SALAD", //2
"VEGETABLES", //3
"CHICKEN", //4
"BEEF", //5
"PIZZA", // 6
"BURGER", //7
"WINGS", //8
"CHEESE", //9
"STRIPS", //10
"SANDWICH", //11
"FAST FOOD", //12
"WRAP", //13
"PASTA", //14
"VEGAN", //15
];
CodePudding user response:
Are you creating new instance of product on every use ? If that is the case then it will be null, what you could do is make it a singleton by using Static keyword so it remains same throughout the app lifecycle. Or you could use new package introduced by flutter "getIt" which will keep record of your product list if you register it with getIt.
CodePudding user response:
I don't think it's a good idea to define the variables like this. It will be better if you use sambast or sqlite to store the products and then you create them on the fly
CodePudding user response:
put List of keywords above the Product Objects. Because you use keywords in Product Object when keywordsList is under Product Objects So make List of keywords above : Product pizza_carnivora = Product("Pizza Carnivora", 23, pizza_hut , [keywords[6],keywords[12]]);
