You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter Floor报错:未向数据库注解添加实体

Floor构建报错:“There are no entities added to the database annotation” 解决办法

执行命令flutter packages pub run build_runner build --delete-conflicting-outputs时出现错误,提示“There are no entities added to the database annotation”,指向package:storeapp/database/myAppDatabase.dart中的MyAppDatabase类,但已在@Database注解中添加多个实体。以下是排查和解决方向:

  • 检查实体类的@Entity注解
    所有加入entities列表的类必须用@Entity()注解标记,Floor才能识别为数据库实体。示例:

    @Entity(tableName: "account")
    class Account {
      // 实体字段及逻辑
    }
    

    若某实体漏加该注解,即使放入entities列表也会被Floor忽略。

  • 确认实体类的导入正确性
    检查myAppDatabase.dart是否已正确导入所有实体类,比如:

    import '../entities/account.dart';
    import '../entities/accounting_class.dart';
    // 其他实体对应的导入语句
    

    导入路径错误或漏导入会导致Floor无法识别实体。

  • 排查命名冲突问题
    比如Order类可能与Dart内置的Order枚举冲突,可通过别名导入解决:

    import '../entities/order.dart' as StoreOrder;
    
    // 在@Database注解中使用别名
    @Database(version: 1, entities: [
      // ...其他实体
      StoreOrder.Order
    ])
    
  • 清理构建缓存后重新执行
    缓存异常可能引发该问题,先清理缓存再重新构建:

    flutter packages pub run build_runner clean
    flutter packages pub run build_runner build --delete-conflicting-outputs
    

内容的提问来源于stack exchange,提问作者FatehiAbdoThabit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 03:09:54