如何在 Back4App 中创建对象列表?

Posted

技术标签:

【中文标题】如何在 Back4App 中创建对象列表?【英文标题】:How to create a List of Object in Back4App? 【发布时间】:2021-10-01 03:37:31 【问题描述】:

我在我的应用程序中使用 back4app,并且我有一个像这样的类,叫做 Meal:

这是我的代码 sn-p:

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 对象列表的类。 并附加到用户我如何实现这一点?

【问题讨论】:

请提供你的代码sn-p你到目前为止所取得的成就 请使用代码 sn-p 编辑您的问题,而不是将其放入 cmets。 你在使用 Parse Server 不是吗? yes 通过 back4app.com 解析服务器 我想为每个检索其每日计划的用户创建一个特定列表 【参考方案1】:

结帐Parse Server Custom Objects。下面是一个示例,说明如何为 Meal 创建自定义对象。

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 中创建对象列表?的主要内容,如果未能解决你的问题,请参考以下文章

在 C++ 中创建一个列表来保存对象

在 Android Studio 中创建一个包含多个对象的列表视图

在Python中创建类似列表的对象

从 Mono 的列表中创建 Flux 的正确方法

如何在java中的每个arraylist中创建具有不同类型对象的arraylist数组?

如何创建对象列表?