Instance of 'Future<dynamic>' in Flutter

36,731

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 :)

Share:
36,731
Maryam Omrani
Author by

Maryam Omrani

Updated on December 10, 2022

Comments

  • Maryam Omrani
    Maryam Omrani over 1 year

    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;
    

    }

    • Sami Kanafani
      Sami Kanafani about 5 years
      show your code please
    • Maryam Omrani
      Maryam Omrani about 5 years
      hi i added the code
  • Maryam Omrani
    Maryam Omrani about 5 years
    i used your first solution but still have the same problem AS : Instance of 'Future'
  • Kamlesh
    Kamlesh over 3 years
    how can we use await in initState function? Thanks.
  • ehhc
    ehhc over 3 years
    @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 stackoverflow.com/a/51901192/11266880 for one solution