2

I have a Async method that I want to call in a stateless widget. While running it it says

Future dynamic Is not a sub type of widget

And then it shows the result after some seconds.How can I stop it? This is my method

    var onvalue = await Firestore.instance
        .collection("userdata")
        .document(user.uid)
        .get();
    if (onvalue.exists) {
      return Navigator.pushReplacement(context,
        MaterialPageRoute(builder: (context) => mainhomepage()),);
    }
    else {
      print("register");
      return  Navigator.pushReplacement(context,
        MaterialPageRoute(builder: (context) => Register()),);
    }
 }

And my main body

Widget build(BuildContext context) {
    var user = Provider.of<User>(context);
    //return either home or authenticate
    return MaterialApp (
        home: user != null?check(user)//Calling the function
            :app()
    );
  }
4
  • Can you post your code @D. Go. Commented May 12, 2020 at 11:37
  • add code whatever’s you tried. Commented May 12, 2020 at 11:37
  • @T.TSage please check it. Commented May 12, 2020 at 11:58
  • @VirenVVarasadiya please check again Commented May 12, 2020 at 11:58

1 Answer 1

6

You can use Futurebuilder.

Widget build(BuildContext context) {
    var user = Provider.of<User>(context);
    //return either home or authenticate
    return MaterialApp (
        home: user != null?
              FutureBuilder(
              future: check(user),
              builder: (_, snapshot) {
                if (snapshot.connectionState == ConnectionState.done) {
                  return Text(snapshot.data);
                } else {
                  return CircularProgressIndicator();
                }
              },
            )
            :app()
          );
      }
Sign up to request clarification or add additional context in comments.

1 Comment

That was very useful for me in my provider with Stateless Widget, Thanks

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.