【问题标题】:Model with nested Map to set on Firebase (json_annotation.dart)在 Firebase 上设置嵌套地图的模型 (json_annotation.dart)
【发布时间】:2020-05-05 05:23:45
【问题描述】:

我的 Firebase 数据库模型如下所示:

dbRef
    |___userId
             |____Cars
                  |____Alfa_Romeo
                  |             |____Year: 1992
                  |             |____Price: 10000
                  |__________Audi
                  |             |____Year: 1998
                  |             |____Price: 3000
                  |___________BMW
                                |____Year: 2001
                                |____Price: 7000

然后我有一辆看起来像的Class Car

import 'package:json_annotation/json_annotation.dart';


part 'car.g.dart';

/// An annotation for the code generator to know that this class needs the
/// JSON serialization logic to be generated.
@JsonSerializable()

class Car {
  Carta(this.model, this.details);

  String model;
  List details;

  factory Car.fromJson(Map<String, dynamic> json) => _$CarFromJson(json);

  Map<String, dynamic> toJson() => _$CarToJson(this);
}

现在,当我将数据设置到 Firebase 时,我得到了这个:

dbRef
    |___userId
              |____Cars
                      |____Alfa_Romeo
                      |____Year: 1992
                      |____Price: 10000

...我希望它像第一张图中的模型。 如何将详细信息嵌套在“模型”子项中? 有人可以帮我解决这个问题吗?

编辑:只是为了确保我清楚我想要什么。 此代码来自 Flutter 团队docs example

import 'address.dart';
import 'package:json_annotation/json_annotation.dart';
part 'user.g.dart';

@JsonSerializable(explicitToJson: true)
class User {
  String firstName;
  Address address;

  User(this.firstName, this.address);

  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
  Map<String, dynamic> toJson() => _$UserToJson(this);
}

此代码生成以下输出:

name: John, address: {street: My st., city: New York}}

我想要实现的是 John 成为一个 Child 键和 Adress 嵌套在里面,因为可能有多个 Adress Array。 Adress 字段应该成为一个 Key(它将是唯一的),然后它将有一个项目映射,每个项目只有 2 个字段描述和价格。

【问题讨论】:

    标签: firebase flutter firebase-realtime-database dart


    【解决方案1】:

    我建议像这个例子一样将细节分解到他们自己的类中,https://flutter.dev/docs/development/data-and-backend/json#generating-code-for-nested-classes

    编辑

    与其使用 JsonSerializable,不如尝试手动映射模型并将剩余属性复制到另一个 Map。

    class Car {
      Car({this.model, this.details});
    
      String model;
      Map<String, dynamic> details;
    
      factory Car.fromJson(Map<String, dynamic> json) {
        Map<String, dynamic> details = Map<String, dynamic>.from(json);
        details.remove('model');
    
        return new Car(
          model: json['model'],
          details: details
        );
      }
    
      Map<String, dynamic> toJson() {
        Map<String, dynamic> json = Map<String, dynamic>.from(details);
        json['model'] = model;
    
        return json;
      }
    }
    

    【讨论】:

    • 我的部分代码来自那里,但是我对这部分感到很困惑,因为我不知道密钥是什么。可能是汽车,可能是型号名称,可能是卡车。与细节相同。它们可能是价格,可能是公里。可能是用户输入的任何内容,唯一的事情是 {String : String} 类型的映射。因此,我需要更多抽象模型,让 mi 将地图放在关键“模型”中。不知道我解释的好不好。
    • 在这种情况下,请尝试将详细信息类型更改为Map&lt;string, dynamic&gt;。如果这仍然不起作用,请尝试创建仅具有 details 属性的 ModelDetails 类。
    • 它不会创建我想要的输出。我将使用颤振团队代码中的示例编辑我的问题,并解释为什么它在我的情况下不起作用。
    • 谢谢,但是这个仍然没有返回嵌套在模型中的细节。我找到了一个解决方法,当我有更多时间时会发布。在那之前,我愿意测试其他解决方案。返回嵌套在模型中的所有细节很重要,因为还有另一个类 User 获取嵌套类 Car 然后我将 User 发布到 Firebase。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 2019-01-10
    • 1970-01-01
    • 2019-07-22
    相关资源
    最近更新 更多