Map不是类型'Map'的子类型。

11 浏览
0 Comments

Map不是类型'Map'的子类型。

我在Firebase上创建了一个实时数据库,并希望将存储的数据显示在Flutter上,但我注意到每当我打开用于显示存储数据的屏幕时,加载了一段时间的CircularProgressIndicator后,控制台上会打印出一个错误消息:“类型'_Map'不是类型'Map'的子类型”。以下是用于显示数据库数据的代码:

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 createState() => _DailyTipsScreenState();

}

class _DailyTipsScreenState extends State {

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(

stream: tipsRef.onValue,

builder: (context, snapshot) {

if (!snapshot.hasData) {

return const Center(

child: CircularProgressIndicator(),

);

}

final tips = >[];

final dataSnapshot = snapshot.data!.snapshot;

(dataSnapshot.value as Map).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 = [];

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

);

},

);

},

),

);

}

}

0