Response.body是空颤振

时间:2020-10-10 13:42:22

标签: android json api flutter dart

我正在尝试通过以下请求从https://fantasy.premierleague.com/api/bootstrap-static/检索数据,但我得到一个空的正文 任何想法如何从这种类型的API检索数据。谢谢 完整的代码是

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  Future<String> getData() async {
    http.Response response = await http.get(
      "https://fantasy.premierleague.com/api/bootstrap-static/",
    );
    var data = response.body;
    print(data);
    return response.body;
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          getData();
        },
      ),
      body: Center(
        child: Text("Hello World"),
      ),
    
    );
  }    
}

问题已解决!!! 在进行全面升级之后,一切都可以正常运行,但我仍然不知道那是怎么回事

升级前扑扑医生

Doctor summary (to see all details, run flutter doctor -v):                                                                  
[√] Flutter (Channel master, 1.21.0-10.0.pre.111, on Microsoft Windows 
[Version 10.0.18363.1082], locale en-US)                                                                                                                                 
[!] Android toolchain - develop for Android devices (Android SDK version 29.0.3)                                            ! Some Android licenses not 
accepted.  To resolve this, run: flutter doctor --android-licenses                      
[√] Android Studio (version 3.6)

[√] VS Code (version 1.50.0)
[!] Connected device
    ! No devices available

! Doctor found issues in 2 categories.

升级后扑扑医生

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel master, 1.23.0-8.0.pre.284, on Microsoft Windows [Version 10.0.18363.1082], locale en-US)

[!] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
! Some Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses
[√] Android Studio (version 3.6)
[√] VS Code (version 1.50.0)
[√] Connected device (1 available)

! Doctor found issues in 1 category.

1 个答案:

答案 0 :(得分:1)

你可以试试吗?

  Future<String> getData() async {
   http.Response response = await http.get("https://fantasy.premierleague.com/api/bootstrap-static/");
var data = response.body;
   print(data );
   return response.body;
  }

以上内容可以肯定地起作用。我对其进行了测试并附加了屏幕截图。 enter image description here

如果要通过另一个变量获取返回值,例如:

String data = getData();

这是行不通的,您需要这样听未来:

String data = getData().then((value) => value);

P.S:这个答案不值得-1。