简体   繁体   English

Flutter Future 返回未来的实例

[英]Flutter Future returning instance of future

I am making an api call to receive data.我正在调用 api 来接收数据。 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(

Your getSecureData method is returning a Future with no type associated, so try including a type to the the Future such as您的getSecureData方法返回没有关联类型的 Future,因此请尝试将类型包含到 Future 中,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM