Reputation: 1272
I want to display the data as list of widgets but I encountered an error Future<dynamic>
is not a subtype of Widget
. Please also check is my getData()
method if it is correct?
getData()
function: getData() async {
final data = await Firestore.instance
.collection('lisofprods')
.document('ac1').get();
DocumentSnapshot snapshot = data.data['name'];
print(snapshot);
return ListTile(
title: Text("$snapshot"),
);
}
Scaffold(
appBar: AppBar(),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
getData(),//here is the error
],
),
),
);
Upvotes: 0
Views: 2474
Reputation: 9625
Function
Future<String> getData() async {
final DocumentSnapshot data = await Firestore.instance
.collection('lisofprods')
.document('ac1').get();
String name = data.data['name'];
return name;
}
Widget
FutureBuilder(
future: getData(),
builder: (context, snapshot){
return ListTile(
title: Text("${snapshot.data.toString()}"),
);
},
),
Check this for full implementation of FutureBuilder
https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html
Upvotes: 4
Reputation: 1609
This is not the proper way to get widget after u got the data, what is happening here is , getData is Async method and you are waiting for the result before returning the widget....
You need to use FutureBuilder : Widget that builds itself based on the latest snapshot of interaction with a Future
For detailed info :FutureBuilder
Upvotes: 4
Reputation: 866
As your "getData()" function marked async, it only returns type "Future". Use Future Builder to achieve the thing
Upvotes: 2