I’m coding the class
class _DashboardState extends State
with SingleTickerProviderStateMixin {
late Future _summaryData;
late Future<List> _chartData;@override initState() { super.initState(); Future<Map> data = fetchData(); _summaryData= data['summaryData']; // ERROR HERE _chartData= data['chartData']; // ERROR HERE } Future<Map> fetchData() async { ... } @override Widget build(BuildContext context) { return FutureBuilder( future: _summaryData, builder: ...); }
but I face an error The operator '[]' isn't defined for the type 'Future<Map<dynamic, dynamic>>'. Try defining the operator '[]'.
.
Please tell me the reason and how to solve? thank you!
2
Answers
The issue here is that you are trying to access summaryData and chartData from a
Future<Map>
and not aMap
. In yourfetchData()
function, make it so that you returnMap
and not aFuture<Map>
like so:This should return you a
Map
within which you should be able to access your required data.Happy Coding 🙂
change your code to the following.
since await cannot be added to init method we create a separate method and mark it async to wait for the api call data.