作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想从Firestore提取数据,并使用ListView以卡的形式显示数据。我愿意从对象中的Firestore中获取数据以在同一屏幕以及其他屏幕上实现可重用性,但是我无法这样做。我创建了一个模型和一个数据库服务来为其服务;
我试图创建一个可以在其中存储HomePage对象的 map 。列表中的每个对象都会创建一张新卡,但是我无法在这些对象中分配值。我尝试使用下面的打印语句,但是没有输出
任何有关如何解决此问题的线索将不胜感激。
模型
class HomePage {
bool confirmed;
DriverDetails driverDetail;
HomePage({this.confirmed, this.driverDetails});
}
class DriverDetails {
String driverUID;
String driverName;
String vehicalNumber;
String vehicalName;
String timeOfReporting;
String placeOfReporting;
LatLng reportingCord;
DriverDetails({
this.driverName,
this.driverUID,
this.placeOfReporting,
this.reportingCord,
this.timeOfReporting,
this.vehicalName,
this.vehicalNumber,
});
}
class DatabaseService {
final Firestore _db = Firestore.instance;
static DriverDetails driverdetails;
static bool confirmed;
Map homeObject = new Map<String, HomePage>();
HomePage home = new HomePage(
confirmed: confirmed,
driverDetail: driverdetails,
);
/// Query a subcollection
Future streamHomePage(FirebaseUser user) async {
var ref = _db
.collection('homepage')
.document(user.uid)
.collection('h')
.document('28032020');
await ref.get(source: Source.serverAndCache).then((ref) => {
ref.data.forEach((index, value) => {
// Prints "index = WE10203 and value Swargate and null"
print(
"index = $index and value ${value['dd']['p']} and ${home.driverDetail}"),
driverdetails.placeOfReporting = value['dd']['p'],
// The below line prints nothing
print("driverdetails = $driverdetails"),
homeObject[index] = home
}),
});
return homeObject;
}
} }
FutureBuilder(
future: databaseService(),
builder: (context, snapshot) {
if (snapshot.hasData) {
stringMap = snapshot.data;
}
return ListView.builder(
itemBuilder: (context, index) {
stringMap.forEach((index, value) => {
print("The stringMap is ${stringMap.keys.toList()}"),
});
return HomepageCards(
user: widget.user,
cardDetails: stringMap[stringMap.keys.toList()[index]],
);
},
itemCount: stringMap.length,
scrollDirection: Axis.vertical,
controller: _controller,
shrinkWrap: true,
);
},
)
databaseService() async {
return DatabaseService().streamHomePage(widget.user);
}
最佳答案
尝试使用如下模型:
import 'package:cloud_firestore/cloud_firestore.dart';
class EmployeeData {
final DocumentReference reference;
String address;
String designation;
String email;
EmployeeData.data(
this.reference, [
this.address,
this.designation,
this.email,
]);
factory EmployeeData.from(DocumentSnapshot document) => EmployeeData.data(
document.reference,
document.data['Address'],
document.data['Designation'],
document.data['Email'],
);
void save() {
reference.setData(toMap());
}
Map<String, dynamic> toMap() {
return {
'address': address,
'designation': designation,
'email': email,
};
}
}
return StreamBuilder<QuerySnapshot>(
stream: Firestore().collection('Workers').snapshots(),
builder: (context, snapshot) {
if (snapshot.data != null) {
// Here u will get list of document snapshots
final List<DocumentSnapshot> documents = snapshot.data.documents;
final List<EmployeeData> employeeDataList = documents
.map((snapshot) => EmployeeData.from(snapshot))
.toList();
// now u can access each document by simply specifying its number
// u can also use list view to display every one of them
return ListView.builder(
itemCount: documents.length,
itemBuilder: (context, int index) => Text(employeeDataList[index].email),
);
} else {
// Show loading indicator here
}
},
);
关于firebase - 如何在flutter中设置模型对象的值并在FutureBuilder ListView中使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61049136/
我是一名优秀的程序员,十分优秀!