如何在 Back4App 中创建对象列表?
How to create a List of Object in Back4App?
我在我的应用程序中使用 back4app,我有一个 class 像这样的 Meal:
这是我的代码片段:
class EatenMeals with ChangeNotifier {
List<Meal> _eatenMeals = [];
List<Meal> get eatenMeals {
return [..._eatenMeals];
}
void addMeal(Meal meal) {
var newMeal = Meal(
id: meal.id,
cal: meal.cal,
catId: meal.catId,
title: meal.title,
duration: meal.duration,
affordability: meal.affordability,
imageUrl: meal.imageUrl,
ingredients: meal.ingredients,
isBreakfast: meal.isDinner,
isDinner: meal.isDinner,
isLunch: meal.isLunch,
steps: meal.steps);
_eatenMeals.add(newMeal);
}
}
现在我想创建一个包含 Meals 对象列表的 class。
并附加到用户 我该如何实现?
结帐Parse Server Custom Objects。以下是您的案例示例,说明如何为膳食创建自定义对象。
class Meal extends ParseObject implements ParseCloneable {
Meal() : super(_keyTableName);
Meal.clone() : this();
/// Looks strangely hacky but due to Flutter not using reflection, we have to
/// mimic a clone
@override
clone(Map map) => Meal.clone()..fromJson(map);
/// Colum names
static const String _keyTableName = 'Meal';
static const String keyCatId = 'catId';
static const String keyTitle = 'title';
static const String keyImgUrl = 'imgUrl';
static const String keyIngredients = 'ingredients';
static const String keySteps = 'steps';
static const String keyCalorie = 'calorie';
static const String keyDuration = 'duration';
static const String keyAffordability = 'affordability';
static const String keyIsBreakfast = 'isBreakfast';
static const String keyIsLunch = 'isLunch';
static const String keyIsDinner = 'isDinner';
/// Getter & Setters
List<String> get catId => get<List<String>>(keyCatId);
set name(List<String> catId) => set<List<String>>(keyCatId, catId);
String get title => get<String>(keyTitle);
set title(String title) => set<String>(keyTitle, title);
Strin> get imgUrl => get<String>(keyImgUrl);
set imgUrl(String imgUrl) => set<String>(keyImgUrl, imgUrl);
List<String> get ingredients => get<List<String>>(keyIngredients);
set ingredients(List<String> ingredients) => set<List<String>>(keyIngredients, ingredients);
List<String> get steps => get<List<String>>(keySteps);
set steps(List<String> steps) => set<List<String>>(keySteps, steps);
num get calorie => get<num>(keyCalorie);
set calorie(num calorie) => set<num>(keyCalorie, calorie);
num get duration => get<num>(keyDuration);
set affordability(num duration) => set<num>(keyDuration, duration);
String get affordability => get<String>(keyAffordability);
set name(String affordability) => set<String>(keyAffordability, affordability);
bool get isBreakfast => get<bool>(keyIsBreakfast);
set isBreakfast(bool isBreakfast) => set<bool>(keyIsBreakfast, isBreakfast);
bool get isLunch => get<bool>(keyIsLunch);
set isLunch(bool isLunch) => set<bool>(keyIsLunch, isLunch);
bool get isDinner => get<bool>(keyIsDinner);
set isDinner(bool isDinner) => set<bool>(keyIsDinner, isDinner);
}
那你就要注册这个子类了
Parse().initialize(
...,
registeredSubClassMap: <String, ParseObjectConstructor>{
'Meal': () => Meal(),
},
);
我在我的应用程序中使用 back4app,我有一个 class 像这样的 Meal:
这是我的代码片段:
class EatenMeals with ChangeNotifier {
List<Meal> _eatenMeals = [];
List<Meal> get eatenMeals {
return [..._eatenMeals];
}
void addMeal(Meal meal) {
var newMeal = Meal(
id: meal.id,
cal: meal.cal,
catId: meal.catId,
title: meal.title,
duration: meal.duration,
affordability: meal.affordability,
imageUrl: meal.imageUrl,
ingredients: meal.ingredients,
isBreakfast: meal.isDinner,
isDinner: meal.isDinner,
isLunch: meal.isLunch,
steps: meal.steps);
_eatenMeals.add(newMeal);
}
}
现在我想创建一个包含 Meals 对象列表的 class。 并附加到用户 我该如何实现?
结帐Parse Server Custom Objects。以下是您的案例示例,说明如何为膳食创建自定义对象。
class Meal extends ParseObject implements ParseCloneable {
Meal() : super(_keyTableName);
Meal.clone() : this();
/// Looks strangely hacky but due to Flutter not using reflection, we have to
/// mimic a clone
@override
clone(Map map) => Meal.clone()..fromJson(map);
/// Colum names
static const String _keyTableName = 'Meal';
static const String keyCatId = 'catId';
static const String keyTitle = 'title';
static const String keyImgUrl = 'imgUrl';
static const String keyIngredients = 'ingredients';
static const String keySteps = 'steps';
static const String keyCalorie = 'calorie';
static const String keyDuration = 'duration';
static const String keyAffordability = 'affordability';
static const String keyIsBreakfast = 'isBreakfast';
static const String keyIsLunch = 'isLunch';
static const String keyIsDinner = 'isDinner';
/// Getter & Setters
List<String> get catId => get<List<String>>(keyCatId);
set name(List<String> catId) => set<List<String>>(keyCatId, catId);
String get title => get<String>(keyTitle);
set title(String title) => set<String>(keyTitle, title);
Strin> get imgUrl => get<String>(keyImgUrl);
set imgUrl(String imgUrl) => set<String>(keyImgUrl, imgUrl);
List<String> get ingredients => get<List<String>>(keyIngredients);
set ingredients(List<String> ingredients) => set<List<String>>(keyIngredients, ingredients);
List<String> get steps => get<List<String>>(keySteps);
set steps(List<String> steps) => set<List<String>>(keySteps, steps);
num get calorie => get<num>(keyCalorie);
set calorie(num calorie) => set<num>(keyCalorie, calorie);
num get duration => get<num>(keyDuration);
set affordability(num duration) => set<num>(keyDuration, duration);
String get affordability => get<String>(keyAffordability);
set name(String affordability) => set<String>(keyAffordability, affordability);
bool get isBreakfast => get<bool>(keyIsBreakfast);
set isBreakfast(bool isBreakfast) => set<bool>(keyIsBreakfast, isBreakfast);
bool get isLunch => get<bool>(keyIsLunch);
set isLunch(bool isLunch) => set<bool>(keyIsLunch, isLunch);
bool get isDinner => get<bool>(keyIsDinner);
set isDinner(bool isDinner) => set<bool>(keyIsDinner, isDinner);
}
那你就要注册这个子类了
Parse().initialize(
...,
registeredSubClassMap: <String, ParseObjectConstructor>{
'Meal': () => Meal(),
},
);