Reputation: 755
I am working to retrieve Firestore Database into Graph - failing to use fromMap() - with .fromMap(Map<String, dynamic> map) placed into Data Models.
What is the best way to get the data in the Widget?
class Sales {
final int saleVal;
final String saleYear;
final String colorVal;
Sales(this.saleVal,this.saleYear,this.colorVal);
Sales.fromMap(Map<String, dynamic> map)
: assert(map['saleVal'] != null),
assert(map['saleYear'] != null),
assert(map['colorVal'] != null),
saleVal = map['saleVal'],
colorVal = map['colorVal'],
saleYear=map['saleYear'];
@override
String toString() => "Record<$saleVal:$saleYear:$colorVal>";
}
Widget _buildBody(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('sales').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return LinearProgressIndicator();
} else {
List<Sales> sales = snapshot.data.docs
.map((documentSnapshot) => Sales.fromMap(snapshot))
.toList();
return _buildChart(context, sales);
}
},
);
}
Upvotes: 0
Views: 593
Reputation: 317372
The error message is telling you that snapshot
is of type AsyncSnapshot and can't be used where a Map is required in the call to Sales.fromMap()
.
If your question is actually asking how to turn a DocumentSnapshot (which is in the variable documentSnapshot
, not snapshot
) into a Map, maybe what you're looking for is using the data() method on it:
Sales.fromMap(documentSnapshot.data())
Upvotes: 0