Search code examples
flutterfirebasefirebase-realtime-databaseflutter-dependencies

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'),
          );
        },
      );
    },
  ),
);
}
}

Solution

  • All you need to do is :

    Map<dynamic,dynamic> snapshotData = snapshot.data!.snapshot.value as dynamic;

    Now you error was gone.