Lalit Rawat
Lalit Rawat

Reputation: 1272

Future<dynamic> is not a subtype of "Widget"

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?

  1. 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"),
      );
    }
  1. Widget
    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

Answers (3)

J. S.
J. S.

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

Sumeet.Jain
Sumeet.Jain

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

SriDatta Yalla
SriDatta Yalla

Reputation: 866

As your "getData()" function marked async, it only returns type "Future". Use Future Builder to achieve the thing

Upvotes: 2

Related Questions