类不会自动生成g.dart文件

j0pj023g  于 2023-09-28  发布在  其他
关注(0)|答案(1)|浏览(93)
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';

part 'person_model.g.dart';

@HiveType(typeId: 1)
class PersonModel extends HiveObject {
  @HiveField(0)
  int personID = UniqueKey().hashCode;

  @HiveField(1)
  String personName = "";

  @HiveField(2)
  double personWeight = 0.0;

  @HiveField(3)
  double personHeight = 0.0;

  @HiveField(4)
  double personBMI = 0.0;

  @HiveField(5)
  bool personHasGoodBMI = false;

  PersonModel();

  PersonModel.create(this.personID, this.personName, this.personWeight,
      this.personHeight, this.personBMI, this.personHasGoodBMI);
}

由于某种原因,当我运行dart run build_runner build时,没有生成person_model.g.dart
我没有错误,实际上它给了我成功,但文件无处可寻。这是我第一次遇到这种事,你能给我解释一下吗?

bqf10yzr

bqf10yzr1#

你好,我通过这样修改你的代码解决了这个问题

import 'package:flutter/material.dart';
import 'package:hive/hive.dart';

part 'person_model.g.dart';

@HiveType(typeId: 100)
class PersonModel {
  @HiveField(0)
  int personID = UniqueKey().hashCode;

  @HiveField(1)
  String personName = "";

  @HiveField(2)
  double personWeight = 0.0;

  @HiveField(3)
  double personHeight = 0.0;

  @HiveField(4)
  double personBMI = 0.0;

  @HiveField(5)
  bool personHasGoodBMI = false;

  PersonModel(this.personID, this.personName, this.personWeight,
      this.personHeight, this.personBMI, this.personHasGoodBMI);
}

使用flutter pub run build_runner build --delete-conflicting-outputs生成.g.dart文件
确保你的文件名为person_model.dart

相关问题