Reputation: 93
I want to fetch data from the firestore and display it in the form of cards using ListView. I am willing the data fetched from firestore in an object for reusability on the same as well as other screens but, I am unable to do so. I created a model and a database service to serve it;
I tried to create a map which can store HomePage objects in it. Each object of the list creates a new card but, I am not able to assign values in the objects. I tried using the print statement below it but no output came there
Any leads on how to solve this problem would be really appreciated.
model
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,
});
}
Database Service
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;
}
} }
Home Page screen
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);
}
Upvotes: 0
Views: 7353
Reputation: 5172
Try using models like this :
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,
};
}
}
so when u got data u can use like this :
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
}
},
);
Upvotes: 1