Flutter에서 이상한 오류가 발생했습니다. 사용자 지정 데이터 모델을 사용하고 있습니다.
class ScanData {
final String userID;
final String companyID;
final String scanID;
final String deviceID;
final String model;
final Map result;
final DateTime dateTime;
final GeoPoint geoPoint;
ScanData(
{this.userID,
this.companyID,
this.scanID,
this.deviceID,
this.model,
this.result,
this.geoPoint,
this.dateTime});
factory ScanData.fromMap(Map data) {
return ScanData(
userID: data['userID'] ?? '',
companyID: data['companyID'] ?? '',
scanID: data['scanID'] ?? '',
deviceID: data['deviceID'] ?? '',
model: data['model'] ?? '',
result: data['result'] ?? {},
dateTime: data['dateTime'] as DateTime ?? DateTime.now(),
geoPoint: data['geoPoint'] as GeoPoint ?? ['77', '28'],
);
}
}
Firestore에서 데이터를 검색하기 위해 Collection 클래스에 메서드를 생성했습니다.
class Collection<T> {
final FirebaseFirestore _db = FirebaseFirestore.instance;
final String path;
CollectionReference ref;
Collection({this.path}) {
ref = _db.collection(path);
}
Future<List<T>> getData() async {
var snapshots = await ref.get();
return snapshots.docs
.map((doc) => Global.models[T](doc.data) as T)
.toList();
}
}
위젯에서 호출하면 데이터 모델을 사용하지 않고 대신 컬렉션을 직접 호출하면 작동하지만 어디를 가든 생성하고 싶지 않고 유형 검사 및 인텔리 젠스에 도움이되기 때문에 훨씬 쉽습니다.
FutureBuilder(
future: Collection<ScanData>(path: 'scans').getData(),
builder: (context, AsyncSnapshot<List<ScanData>> snapshot) {
if (!snapshot.hasData || snapshot.hasError) {
return Container(
alignment: Alignment.center,
child: CircularProgressIndicator());
} else {
return _buildScansList(snapshot.data);
}
},
)
글로벌 :
class Global {
static final Map models = {
ScanData: (data) => ScanData.fromMap(data),
};
}
오류 (Globals fromMap에서 발생) :
Exception has occurred.
_TypeError (type '() => Map<String, dynamic>' is not a subtype of type 'Map<dynamic, dynamic>')
ScanData 모델에 다음을 넣어 보았습니다.
factory ScanData.fromMap(Map<String,dynamic> data) {...}
그러나 이것은 또 다른 오류를 제공합니다.
Exception has occurred.
_TypeError (type '() => Map<String, dynamic>' is not a subtype of type 'Map<String, dynamic>')
: | 감사합니다.
Collection 클래스의 getData () 메서드에서 doc.data
속성이 아니라 메서드이므로 ()
.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다