0

I want to get two parameters from createUser response. I have a raisedbutton and i want to use FutureBuilder when onPressed.
future property works well, but i can not get in builder property. I tried everything but i could not find solution.

...
RaisedButton(
        elevation: 5.0,
        onPressed: (){
          FutureBuilder(
              future: createUser(usernameController.text, emailController.text, passwordController.text, '1', 1),
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  saveLogin(snapshot.data.userUnique);
                  nextScreen(
                      context,
                      UserDetail(
                        userUnique: snapshot.data.userUnique,
                      ));
                } else {
                  return MyHomePage();
                }
                return MyHomePage();
              });
        },
...


Future createUser(String username, String password, String email,
    String logintype, int userUnique) async {
  String url =
      "http://api.xx.com/api/register?username=$username&password=$password&email=$email&logintype=$logintype&userUnique=$userUnique";

  final response =
      await http.get(url, headers: {'Content-Type': 'application/json'});
  return registerFromJson(utf8.decode(response.bodyBytes));
jancooth
  • 555
  • 1
  • 10
  • 25
  • FutureBuilder is a widget and I don't think it's what you want here. I think you need to take the future here and use async/await or .then((){}) on that future. Putting a widget inside the onPressed method doesn't do anything and it doesn't really make sense – Morez May 13 '20 at 13:42
  • I want to get two parameters from createUser response that why i use futurebuilder after onpressed. – jancooth May 13 '20 at 13:46
  • you need to Navigate to other classes if that's what you aiming for here like Navigator.push( context, MaterialPageRoute(builder: (context) => MyHomePage()), ); – Mousa Alribi May 13 '20 at 14:01
  • @MousaAlribi there is no problem navigate to other class. I want to decide navigate after createUser method response. – jancooth May 13 '20 at 14:05
  • don't use futurebuild try to Await createUser and put everything you want to do after it .. it will not run until you get the creatuser returns – Mousa Alribi May 13 '20 at 14:19
  • @MousaAlribi, how to handle createUser responseCode and userUnique could you please give me some code example. – jancooth May 13 '20 at 14:24
  • what do you want to do with your `FutureBuilder` which is not attached to any parent widget? i mean there is no: `SomeParentWidget(child: FutureBuilder(...))` – pskink May 13 '20 at 14:27
  • @pskink I have a method " createUser " and this method returns two parameter(responseCode and userUnique). I try to navigate page according to responseCode. Thats it. – jancooth May 13 '20 at 14:30
  • so use `Future.then()` or `async` / `await`, more: https://dart.dev/codelabs/async-await - as i said your `FutureBuilder` hangs in the air: it is not attached to any parent widget – pskink May 13 '20 at 14:31
  • `createUser(usernameController.text, emailController.text, passwordController.text, '1', 1).then((value) => print(value));` – pskink May 13 '20 at 14:37
  • @pskink if i use async / await, how to handle userUnique and responseCode from my method. – jancooth May 13 '20 at 14:40
  • use .then it will return the values from your function and you can handle it as you like – Mousa Alribi May 13 '20 at 14:43
  • your method cannot return two `Future`s - it returns one `Future` - most likely `Future` or something like that - you did not post `createUser()` method code so i can only guess... – pskink May 13 '20 at 14:43
  • @pskink i updated my code. You can see my createUser method. – jancooth May 13 '20 at 14:49
  • so it returns `registerFromJson(...)` – pskink May 13 '20 at 14:52
  • @pskink registerFromJson has responseCode and userUnique properties. – jancooth May 13 '20 at 14:55

1 Answers1

2

I changed FutureBuilder to async / await method

thanks for your help @pskink

onPressed: () async {
                            var xx = await createUser( usernameController.text, emailController.text, passwordController.text, '1',userUnique());
                            print(xx.responseCode.toString());
                            print(xx.userUnique.toString());
                            saveLogin(userUnique());

                          },
jancooth
  • 555
  • 1
  • 10
  • 25