gpt4 book ai didi

firebase - 如何在flutter中设置模型对象的值并在FutureBuilder ListView中使用它?

转载 作者:行者123 更新时间:2023-12-03 04:53:12 25 4
gpt4 key购买 nike

我想从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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com