我遵循了Flutter Floor教程,这是我的数据库文件
import 'package:floor/floor.dart';
import 'package:news_app/database/daos/Userdao.dart';
import 'package:news_app/database/entities/User.dart';
part "AppDatabase.g.dart";
@Database(entities: [User], version: 1)
abstract class ApplicationDatabase extends FloorDatabase {
Userdao get userDao;
}
这是我的dao文件,
import 'package:floor/floor.dart';
import 'package:news_app/database/entities/User.dart';
@dao
abstract class Userdao{
@Query("select * from table_user")
Future<List<User>> getUsersList();
@insert
Future<void> insertUser(User user);
}
这是我的实体文件,
import 'package:floor/floor.dart';
@Entity(tableName: 'table_user')
class User{
@PrimaryKey(autoGenerate: true)
int id;
String name;
String password;
User({this.id,this.name,this.password});
}
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
fluttertoast: 3.1.3
shared_preferences: 0.5.6
retrofit: any
logger: any
dio: 3.0.8
http: 0.12.0+4
json_serializable: any
floor: ^0.9.0
dev_dependencies:
flutter_test:
sdk: flutter
retrofit_generator: any
build_runner: any
floor_generator: ^0.9.0
每当我尝试运行此命令 flutter pub run build_runner build 命令时,都会出现此错误
[INFO]正在运行构建... [INFO]经过1.7秒,完成了0/1个操作。 [警告] lib / database / ApplicationDatabase.dart上的floor_generator:floor_generator: 缺少“部分'ApplicationDatabase.g.dart';”。
答案 0 :(得分:3)
对于将来的读者,要创建database.g.dart
,您必须在终端中运行以下命令,
flutter packages pub run build_runner build
或要在文件更改时自动运行它,请使用
flutter packages pub run build_runner watch
要阅读完整的程序,请访问floor documentation
答案 1 :(得分:0)
您是否使用相同名称的数据库文件
喜欢 appdatabase.dart 和 appdatabase.g.dart