0

How would you return a Future widget into a StatelessWidget? I've tried something myself but it gives the error: "type 'Future' is not a subtype of type 'Widget?'". Here is the code

recieveMessage() async {
var x = 'Test';
  return Text(x);
}

class Test extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: recieveMessage(),
    );
  }
}

Thanks in advance.

Teun
  • 37
  • 9
  • 1
    Does this answer your question? [What is a Future and how do I use it?](https://stackoverflow.com/questions/63017280/what-is-a-future-and-how-do-i-use-it) – nvoigt Feb 26 '21 at 10:10
  • No it doesn't sadly :( – Teun Feb 26 '21 at 10:51

1 Answers1

1

you should use the future builder widget

Future<widget> recieveMessage() async {
 var x = 'Test';
 return Text(x);
}



FutureBuilder(
 future:recieveMessage()
 builder: (context, snapshot) {
  if (snapshot.connectionState == snapshot.waiting)       
    return CircularProgressIndicator();
  
  return snapshot.data
Ibrahim Ali
  • 2,083
  • 2
  • 15
  • 36