Flutter Future returning instance of future

Issue

I am making an api call to receive data. When I make the call, I get back an instance of future. Why does this still return an instance of future instead of waiting for the data?

Here is my networking file

class NetworkHelper {
  NetworkHelper(this.url);

  final String url;

Future getSecureData(String token) async {
    http.Response response = await http.post(
      Uri.parse(url),
      headers: {
        HttpHeaders.authorizationHeader: token,
      },
    );
    var data = response;
    return data;
  }

Here is my Account Settings file

class AccountSettings extends StatefulWidget {
  const AccountSettings({Key? key}) : super(key: key);
  static const String id = 'account_settings';

  @override
  State<AccountSettings> createState() => _AccountSettingsState();
}

class _AccountSettingsState extends State<AccountSettings> {
  var userData;

  @override
  void initState() {
    super.initState();

    getUserData();
  }

  Future getUserData() async {
    var token = await SecureStorage.getAccessToken();
    var jwttoken = 'JWT ' + token!;
    NetworkHelper networkHelper =
        NetworkHelper('http://localhost:8000/auth/users/me');
    userData = await networkHelper.getSecureData(jwttoken);
  }

  @override
  Widget build(BuildContext context) {
    print(userData);
    return Scaffold(

Solution

Your getSecureData method is returning a Future with no type associated, so try including a type to the the Future such as

Future<SomeType> getSecureData() async {
    //..
}

Answered By – Pheepster

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published