今天在做数据库查询时,能返回正确的查询条数和实体对象,但是实体对象里都是空值,最后发现是因为实体对象没有创建构造函数导致的,新建一个构造函数就完美解决。记录一下。
附上我的实体类对象代码:
import 'package:floor/floor.dart';
@entity
class Beacon {
@PrimaryKey(autoGenerate: true)
int? id;
String? major;
String? minor;
double? dx;
double? dy;
String? name;
String? floorName;
bool? indoor = true;
int? gid;
int? userid;
String? projectionId;
String? floorid;
// 是否有修改
bool? modified = false;
String? mapId;
void clear() {
major = '';
minor = '';
dx = 0.0;
dy = 0.0;
name = null;
floorName = null;
gid = 1;
projectionId = null;
floorid = null;
modified = false;
}
Beacon(
{this.id,
this.major,
this.minor,
this.dx,
this.dy,
this.name,
this.floorName,
this.floorid,
this.indoor,
this.gid,
this.userid,
this.projectionId,
this.modified,
this.mapId});
@override
bool operator ==(other) {
if (other is! Beacon) {
return false;
}
final Beacon beacon = other;
return beacon.major == this.major &&
beacon.minor == this.minor &&
beacon.dx == this.dx &&
beacon.dy == this.dy &&
this.name == beacon.name &&
this.floorName == beacon.floorName &&
this.indoor == beacon.indoor &&
this.gid == beacon.gid &&
this.floorid == beacon.floorid &&
this.projectionId == beacon.projectionId;
}
factory Beacon.fromJson(Map<String, dynamic> json) {
String? major = json['deviceAttributes']['major'] as String?;
String? minor = json['deviceAttributes']['minor'] as String?;
double dx = (json['deviceAttributes']['x'] as num).toDouble();
double dy = (json['deviceAttributes']['y'] as num).toDouble();
bool indoor =
json['deviceAttributes']['place'] as String == '室内' ? true : false;
String projectionId = json['proId'] as String;
String mapId = json['mapId'] as String;
int userid = json['userId'] as int;
String name = json['deviceName'];
int gid = json['level'] ?? 1;
String floorid = json['floorId'] as String;
bool modified = false;
Beacon beacon = Beacon(
gid: gid,
major: major,
minor: minor,
dx: dx,
dy: dy,
indoor: indoor,
projectionId: projectionId,
mapId: mapId,
userid: userid,
name: name,
floorid: floorid,
modified: modified);
return beacon;
}
}
网友评论