【问题标题】:How to use Customer TypeAdapter (Hive/Flutter)?如何使用客户类型适配器(Hive/Flutter)?
【发布时间】:2021-07-01 06:02:18
【问题描述】:

我不太确定如何在 Flutter 中使用 Hive DB。我的意思是我有一个 ```WooCustomer`` 模型类,我想将它存储在本地(一旦客户登录)。

我的问题是,我必须将 WooCustomer 转换为 HiveObject 然后创建 TypeAdapter 还是直接创建 TypeAdapter<WooCustomer>

PS:WooCustomer 是一个外部 pkg。

这是实现TypeAdapter<WooCustomer>的正确方法吗?

class DatabaseAdapterService extends TypeAdapter<WooCustomer> {
  @override
  final int typeId = 0;

  @override
  WooCustomer read(BinaryReader reader) {
    return WooCustomer()
      ..id = reader.read()
      ..username = reader.read()
      ..firstName = reader.read()
      ..lastName = reader.read()
      ..email = reader.read()
      ..password = reader.read()
      ..avatarUrl = reader.read()
      ..role = reader.read()
      ..dateCreated = reader.read()
      ..dateCreatedGmt = reader.read()
      ..dateModified = reader.read()
      ..dateModifiedGmt = reader.read()
      ..isPayingCustomer = reader.read()
      ..links = reader.read()
      ..metaData = reader.read()
      ..billing = reader.read()
      ..shipping = reader.read();
  }

  @override
  void write(BinaryWriter writer, WooCustomer customer) {
    writer.write(customer.username);
    writer.write(customer.id);
    writer.write(customer.firstName);
    writer.write(customer.lastName);
    writer.write(customer.email);
    writer.write(customer.password);
    writer.write(customer.links);
    writer.write(customer.avatarUrl);
    writer.write(customer.role);
    writer.write(customer.metaData);
    writer.write(customer.dateCreated);
    writer.write(customer.dateCreatedGmt);
    writer.write(customer.dateModified);
    writer.write(customer.dateModified);
    writer.write(customer.dateModifiedGmt);
    writer.write(customer.isPayingCustomer);
    writer.write(customer.billing);
    writer.write(customer.shipping);
  }
}

【问题讨论】:

    标签: flutter dart woocommerce flutter-hive


    【解决方案1】:

    是的,你总是可以做到的。 首先获取这些插件,hive, hive_flutter 在 pubspec.yaml 中的依赖项中。 并且在 pubspec 文件中还有一段 dev dependencies 有添加,hive_generator: ^1.1.0 build_runner: ^2.0.1

    你是很好的开始...... 首先以这种格式编写您的课程,

    import 'package:hive/hive.dart'; //import Hive
    
    part 'weather_model.g.dart'; 
    // Specify the Location of the type-adapter's Location (specifically)
    //Add the .g.dart extension
    // at last of the same File name you are using
    // and add "part" keyword before all.
    
    @HiveType(typeId: 0) //Add this Line
    class WeatherModel {
      @HiveField(0) //Add this Line, From Index 0... and so on.
      String user_city; // Your Class Materials...
      @HiveField(1) //Add this Line, From Index 0... and so on.
      bool celciusMetric;
    
      
    
      WeatherModel(
          {this.user_city,
          this.celciusMetric}); //Constructor here.. 
    
      // flutter packages pub run build_runner build
    }
    

    阅读完上面的全部注释代码后, 并根据您的用途实施。 转到您的终端并在那里写信,flutter packages pub run build_runner build --delete-conflicting-outputs 就可以了,您会发现

    现在转到您的main.dart 并导入 hive 的所有内容。

    【讨论】:

      猜你喜欢
      • 2020-10-31
      • 2021-09-24
      • 2021-12-12
      • 2021-11-06
      • 1970-01-01
      • 2021-04-10
      • 2017-10-14
      • 2022-11-12
      • 1970-01-01
      相关资源
      最近更新 更多