body:
StreamBuilder<QuerySnapshot>(
stream: users.snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
snapshot.data.docs.forEach((element) {
_datasource.add(element['id']; // datasource is currently a global variable. I'd rather not do that.
});
return UserDataGrid();
},
),
[main.dart]
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Scaffold(
appBar: AppBar(
title: Text('DataGrid'),
),
body: StreamBuilder<QuerySnapshot>(
stream: employeeDataGridSource.getStream(),
builder: (context, snapshot) {
employeeDataGridSource.buildStream(snapshot);
return SfDataGrid(
source: employeeDataGridSource,
columnWidthMode: ColumnWidthMode.fill,
columns: [
GridNumericColumn(mappingName: 'employeeID', headerText: 'ID'),
GridTextColumn(mappingName: 'employeeName', headerText: 'Name'),
GridTextColumn(mappingName: 'designation', headerText: 'Role'),
GridNumericColumn(mappingName: 'salary', headerText: 'Salary'),
],
);
}
),
),
);
}
|
class EmployeeDataGridSource extends DataGridSource<Employee> {
CollectionReference collection;
EmployeeDataGridSource() {
collection = FirebaseFirestore.instance.collection('employees');
}
Stream<QuerySnapshot> getStream() {
return collection.orderBy('employeeID', descending: false).snapshots();
}
Future<Void> buildStream(AsyncSnapshot snapShot) async {
if (snapShot.hasError ||
snapShot.data == null ||
snapShot.data.docs.length == 0) {
return Future<Void>.value();
}
await Future.forEach(snapShot.data.docs, (element) {
final Employee data = Employee.fromSnapshot(element);
if (!employees.any((element) => element.employeeID == data.employeeID)) {
employees.add(data);
}
});
updateDataGridDataSource();
return Future<Void>.value();
}
void updateDataGridDataSource() {
notifyListeners();
}
}
|
@override
void initState() {
// fetch users collection and watch for changes
CollectionReference reference = Firestore.instance.collection('users');
reference.snapshots().listen((querySnapshot) {
querySnapshot.docChanges.forEach((change) {
DocumentSnapshot doc = change.doc;
String docId = doc.id;
Person person = new Person(
id: doc.data()['id'],
displayName: doc.data()['name'],
role: new CalendarResource(id: doc.data()['role']),
balance: doc.data()['balance']
);
switch(change.type) {
case (DocumentChangeType.added): {
print("added: " + change.doc.data().toString());
_personMap.putIfAbsent(docId, () => person);
break;
}
case (DocumentChangeType.removed): {
print("removed: " + change.doc.data().toString());
_personMap.remove(docId);
break;
}
case (DocumentChangeType.modified): {
print("modified: " + change.doc.data().toString());
_personMap.update(docId, (value) => person);
break;
}
}
_personData = _personMap.values.toList();
_personData.sort((a, b) => a.displayName.compareTo(b.displayName));
_personDataSource.updateDataGridSource();
});
});
Hi @Grady can you please provide a full working sample (example) to test, I tried in a lot of ways unsuccessfully :(
Thanks in a advance!