3

I'm getting an int Id from server which in HTTP request in flutter, it is OK and I want to save this value in an variable , but when i call the returned value i get this error Instance of 'Future' , any suggestion ?? Here is the code

                    child: RaisedButton(onPressed:() {
                    CurrentPrjId= SaveProject();
                    print("CurrentPrjId Is saved CurrentPrjId :::::::: " );
                    print(CurrentPrjId);
                    Navigator.of(context).pushReplacementNamed('/AllUSerStories');
                }
//Save Project

  Future SaveProject() async {

//SaveProjectResult has the id of the current created project which i need that for its user stories
var SaveProjectResult = await GetPostProjects().createProject(_ProjectnameController.text, _CustomernameController.text, _dropNewItem, _dropNewItem2, _StartDateController.text, _EndDateController.text,);
print(":::::::::::::::::::::::::::::::The Project Is Created And Here Its ID In SaveProject:::::::::");
print(SaveProjectResult);
return SaveProjectResult;

}

Maryam Omrani
  • 41
  • 1
  • 1
  • 4

3 Answers3

15

Without having your code, it's hard to guess, whats going on.. But you may try to wait for the completion of the future.. so do this:

methOdThatLoadsId().then((id) => print("Id that was loaded: $id"));

or

var id = await methodThatLoadsId();

Good luck :)

ehhc
  • 537
  • 5
  • 14
  • i used your first solution but still have the same problem AS : Instance of 'Future' – Maryam Omrani Apr 18 '19 at 13:20
  • how can we use await in initState function? Thanks. – Kamlesh Oct 27 '20 at 16:00
  • @Kamlesh as far as i know, that's not possible at all. Nontheless, there are several ways on how you can work with async-tasks in initState. See https://stackoverflow.com/a/51901192/11266880 for one solution – ehhc Oct 27 '20 at 16:21
0

sorry for the late reply. You've added the await at the wrong place unfortunately. Try something like this:

                   child: RaisedButton(onPressed:() async {
                    CurrentPrjId=  await SaveProject();
                    print("CurrentPrjId Is saved CurrentPrjId :::::::: " );
                    print(CurrentPrjId);
                    Navigator.of(context).pushReplacementNamed('/AllUSerStories');
                }
//Save Project

  Future SaveProject() async {

//SaveProjectResult has the id of the current created project which i need that for its user stories
var SaveProjectResult = await GetPostProjects().createProject(_ProjectnameController.text, _CustomernameController.text, _dropNewItem, _dropNewItem2, _StartDateController.text, _EndDateController.text,);
print(":::::::::::::::::::::::::::::::The Project Is Created And Here Its ID In SaveProject:::::::::");
print(SaveProjectResult);
return SaveProjectResult;

Or shorter:

                   child: RaisedButton(onPressed:() async {
                    CurrentPrjId=  await GetPostProjects().createProject(_ProjectnameController.text, _CustomernameController.text, _dropNewItem, _dropNewItem2, _StartDateController.text, _EndDateController.text,);
                    print("CurrentPrjId Is saved CurrentPrjId :::::::: " );
                    print(CurrentPrjId);
                    Navigator.of(context).pushReplacementNamed('/AllUSerStories');
                }
ehhc
  • 537
  • 5
  • 14
0

You have to use a FutureBuilder widget to get Future value from an async call.

CurrentPrjId=FutureBuilder<String>(
    future: SaveProject(),
    builder: (context, snapshot) {
      if (snapshot.hasData) return Text(snapshot.data);
      else if (snapshot.hasError) return Text(snapshot.error);
      return return Text("Await for data");
    },
  );