I want to create a map in Firebase and get the slider values from there. But it gives an error. "type ‘_Map<String, dynamic>’ is not a subtype of type ‘Map<Double, Double>’ in type cast
"
Expanded(
child: StreamBuilder<DocumentSnapshot>(
stream: FirebaseFirestore.instance
.collection("deger").doc("1")
.snapshots(),
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
final Map<Double, Double> map = snapshot.data!["item"] as Map<Double, Double>;
return VerticalSlider(
min: parameters.minDecibels,
max: parameters.maxDecibels,
value: map as double,
onChanged: band.setGain,
);
}),
),
I couldn’t find a solution.
2
Answers
type '_Map<dynamic, dynamic>' is not a subtype of type 'double' it gives this error.
The keys in the
items
are not a number. They’re strings like"item1"
,"item2"
, etc.If you want to interpret just the number part of those (i.e.
1
,2
, etc), you will have to parse the key in your code.Something like:
So the above first removes the
"item"
prefix from the string, and then converts the remained to a number.A complete example I tested in DartPad: