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.

2
  • 1
    Does this answer your question? What is a Future and how do I use it? Commented Feb 26, 2021 at 10:10
  • No it doesn't sadly :( Commented Feb 26, 2021 at 10:51

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer. But I'm getting now this error message: "Another exception was thrown: No Directionality widget found."
@Teun wrap your whole widget tree with the MaterialApp widget...
Yes I got it. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.