Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map<Object? Object> is not a subtype of type 'Map<String, dynamic>

I created a realtime database on firebase and wanted the data stored to be displayed on flutter but I noticed that whenever I opened the screen meant to display the data stored, after the CircularProgressIndicator for a while, i get an error "type '_Map<Object?, Object?>' is not a subtype of type 'Map<String?, dynamic>' in type cast" printed on the console.

This is my code to display the data from the database.

    import 'package:flutter/material.dart';
    import 'package:firebase_database/firebase_database.dart';

    class DailyTipsScreen extends StatefulWidget {
    const DailyTipsScreen({Key? key}) : super(key: key);

    @override
    State<DailyTipsScreen> createState() => _DailyTipsScreenState();
 }

 class _DailyTipsScreenState extends State<DailyTipsScreen> {

late DatabaseReference tipsRef;

@override
void initState() {
super.initState();
tipsRef = FirebaseDatabase.instance.ref().child('tips');
}

@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: const Text('Tips'),
  ),
  body: StreamBuilder<DatabaseEvent>(
    stream: tipsRef.onValue,
    builder: (context, snapshot) {
      if (!snapshot.hasData) {
        return const Center(
          child: CircularProgressIndicator(),
        );
      }
      final tips = <Map<String, dynamic>>[];
      final dataSnapshot = snapshot.data!.snapshot;
      (dataSnapshot.value as Map<String?, dynamic>).forEach((key, value) {
        tips.add(value);
      });

      return ListView.builder(
        itemCount: tips.length,
        itemBuilder: (context, index) {
          final tip = tips[index];
          final homeTeamName = tip['home_team_name'];
          final awayTeamName = tip['away_team_name'];
          final scoreFulltime = tip['score_fulltime'];
          final bettingTips = tip['betting_tips'];

          final odds = <String>[];
          bettingTips.forEach((key, value) {
            odds.add(value['odds']);
          });
          final oddsString = odds.join(' / ');

          return ListTile(
            title: Text('$homeTeamName vs $awayTeamName'),
            subtitle: Text('Score: $scoreFulltime, Odds: $oddsString'),
          );
        },
      );
    },
  ),
);
}
}
like image 954
Jesse Josiah Avatar asked May 13 '26 11:05

Jesse Josiah


2 Answers

My workaround was this cast:

myJson = Map<String, dynamic>.from (snapshot.value as Map);

This doesn't look nice, but I will use this until anything more elegant comes up.

like image 67
Raja Avatar answered May 19 '26 05:05

Raja


You should cast your data to the type you want. Map<Object?, Object> can be cast to the type you want by the following code:

map.cast<String, dynamic>()

So, you can solve this problem by changing the following code

final dataSnapshot = snapshot.data!.snapshot;
      (dataSnapshot.value as Map<String?, dynamic>).forEach((key, value) {
        tips.add(value);
      });

to

Map<Object?, Object> dataSnapshot = snapshot.data!.snapshot;
Map<String?, dynamic> castedData = dataSnapshot.cast<String?, dynamic>();
      castedData.values.forEach((key, value) {
        tips.add(value);
      });
like image 39
Just a Person Avatar answered May 19 '26 05:05

Just a Person