如何在Isar DB Flutter中保存这个嵌套类

4szc88ey  于 2023-11-21  发布在  Flutter
关注(0)|答案(1)|浏览(185)

我有以下4个类。在这个,只有ProductGroup被保存,ProductVariantProductSizeProductColor没有存储。请帮助我。

  • product_group.dart*
@Collection()
class ProductGroup {
  late Id id;

  @Index(caseSensitive: false)
  late String productGroupName;

  final productVariants = IsarLinks<ProductVariant>();

  ProductGroup();

  ProductGroup.fromJson(Map<String, dynamic> json) {
    id = json['Id'];
    productGroupName = json['PG'];
    if (json['Ps'] != null) {
      json['Ps'].forEach((variant) {
        productVariants.add(ProductVariant.fromJson(variant));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['Id'] = id;
    data['PG'] = productGroupName;
    data['Ps'] = productVariants.map((variant) => variant.toJson()).toList();
    return data;
  }
}

字符串

  • product_variant.dart*
@Collection()
class ProductVariant {
  late Id id;
  late String variantName;
  final productSizes = IsarLinks<ProductSize>();

  ProductVariant();

  ProductVariant.fromJson(Map<String, dynamic> json) {
    id = json['Id'];
    variantName = json['St'];
    if (json['Ss'] != null) {
      json['Ss'].forEach((v) {
        productSizes.add(ProductSize.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['Id'] = id;
    data['St'] = variantName;
    data['Ss'] = productSizes.map((v) => v.toJson()).toList();
    return data;
  }
}

  • product_size.dart*
@Collection()
class ProductSize {
  late Id id;
  late String size;
  final productColors = IsarLinks<ProductColor>();

  ProductSize();

  ProductSize.fromJson(Map<String, dynamic> json) {
    id = json['Id'];
    size = json['S'];
    if (json['Cs'] != null) {
      json['Cs'].forEach((v) {
        productColors.add(ProductColor.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['Id'] = id;
    data['S'] = size;
    data['Cs'] = productColors.map((color) => color.toJson()).toList();
    return data;
  }
}

  • product_color.dart*
@Collection()
class ProductColor {
  late Id id;
  late String colorName;
  late String colorHexCode;

  ProductColor();

  ProductColor.fromJson(Map<String, dynamic> json) {
    id = json['Id'];
    colorName = json['C'];
    colorHexCode = json['CC'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['Id'] = id;
    data['C'] = colorName;
    data['CC'] = colorHexCode;
    return data;
  }
}


我正在解析json并将其保存在Isar中

convertJsonToIsar() async {
    try {
      // For testing purposes, loading Json from assets, in Prod, Json will be fetched from server
      final String response = await rootBundle.loadString('assets/pr_dump.json');
      final data = await json.decode(response);

      List<ProductGroup> productGroupList = [];
      data.forEach((item) {
        productGroupList.add(ProductGroup.fromJson(item));
      });

      Isar _isar = getIsar();

      _isar.writeTxnSync(() {
         _isar.productGroups.putAllSync(productGroupList, saveLinks: true);
      });

    } catch (e) {
      // Handle Error
      print('Caught Error');
      print(e.toString());
      return 0;
    }
  }


只有ProductGroup被存储,ProductVariantProductSizeProductColor没有被存储。请帮助我。

23c0lvtd

23c0lvtd1#

正如Isar文档中所述:“链接是惰性的,所以你需要告诉IsarLink显式地加载或保存值。你可以通过调用linkProperty.load()linkProperty.save()来做到这一点。”
所以每次你想保存一个IsarLink属性时,你需要调用save()函数。
因此,使用您的示例,假设您有一个名为ProductGroup productGroup的变量和一个名为isar的Isar示例。要将其保存到Isar上,您需要执行以下操作:

await isar.writeTxn(() async {
  await isar.productGroups.put(productGroup);
  await productGroup.productVariant.save();

// ... and so on
    });

字符串

相关问题