I have 2 Entities in my Flutter project. User and Team. I add User as an Entity and generate the autogenerated code successfully.
After this, i add the Team Entity, with the same way and got the error:
You are trying to change an object which is not an entity
for my insert method.
My User Entity:
@entity
class User {
@PrimaryKey(autoGenerate: true)
int? id;
String firstName = "";
User({
this.id,
required this.firstName,
});
Map<String, dynamic> toJson() {
return {
'id': id,
'firstName': firstName,
};
}
}
My user_dao:
@dao
abstract class UserDao {
@Query('SELECT * FROM user')
Future<List<User>> findAllUsers();
@Query('SELECT * FROM user WHERE id = :id')
Future<User?> findUserById(int id);
@insert
Future<void> insertUser(User user);
@update
Future<void> updateUser(User user);
@delete
Future<void> deleteUser(User user);
}
And my database:
@Database(version: 1, entities: [User])
abstract class AppDatabase extends FloorDatabase {
UserDao get userDao;
}
This is working pretty fine.
Now i add my Team Entity:
@entity
class Team {
@PrimaryKey(autoGenerate: true)
int? id;
String name = "";
Team({
this.id,
required this.name
});
Map<String, dynamic> toJson() {
return {
'id': id,
'name': name,
};
}
}
And my team_dao as well:
@dao
abstract class TeamDao {
@Query('SELECT * FROM team')
Future<List<Team>> findAllTeams();
@Query('SELECT * FROM team WHERE id = :id')
Future<Team?> findTeamById(int id);
@insert
Future<void> insertTeam(Team team);
@update
Future<void> updateTeam(Team team);
@delete
Future<void> deleteTeam(Team team);
}
After all, i add the new dao to the database:
@Database(version: 1, entities: [User])
@Database(version: 2, entities: [Team])
abstract class AppDatabase extends FloorDatabase {
UserDao get userDao;
TeamDao get teamDao;
}
But now, i got the error message for my team_doa
You are trying to change an object which is not an entity
Future<void> insertTeam(Team team);
But i don't understand why? Or did i some mistake?
ok, i found the problem.
I had to change:
to
and the database.g.dart will be generated