Reputation: 1
i took the code from github, still not working
The following _TypeError was thrown building FutureBuilder(dirty, state: _FutureBuilderState#a936c): type 'List' is not a subtype of type 'Map<dynamic, dynamic>'
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
class Home extends StatefulWidget {
Home({Key key, this.title}) : super(key: key);
final String title;
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
final dbRef = FirebaseDatabase.instance.reference().child("qs");
List declaration Here
List<Map<dynamic,dynamic>> lists = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('nothing'),
),
body: FutureBuilder(
future: dbRef.once(),
builder: (context, AsyncSnapshot<DataSnapshot> snapshot) {
if (snapshot.hasData) {
lists.clear();
Here's the MAP
Map<dynamic,dynamic> values = snapshot.data.value;
values.forEach((key, values) {
lists.add(values);
});
return new ListView.builder(
shrinkWrap: true,
itemCount: lists.length,
itemBuilder: (BuildContext context[enter image description here][1], int index) {
return Card(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("Name: " + lists[index]["name"]),
Text("Age: " + lists[index]["age"]),
Text("Type: " + lists[index]["type"]),
],
),
);
});
}
return CircularProgressIndicator();
}));
}
}
Upvotes: 0
Views: 297
Reputation: 7706
It looks like you're assigning a value of type List<dynamic>
to a variable of type Map<dynamic,dynamic>
.
You can change the code below:
Map<dynamic,dynamic> values = snapshot.data.value;
values.forEach((key, values) {
lists.add(values);
});
to this:
list = snapshot.data.value.map((value) => value as Map<dynamic, dynamic>).toList();
The code above maps the snapshot.data.value
into a List
with each list item casted as a Map<dynamic, dynamic>
.
Upvotes: 1