【问题标题】:Unhandled Exception: type 'Future<Response>' is not a subtype of type 'String'未处理的异常:“Future<Response>”类型不是“String”类型的子类型
【发布时间】:2020-11-19 02:52:47
【问题描述】:

我正在尝试从 Facebook 获取数据。 http.get() 返回 Future 类型的数据。该数据被传递给第二个函数,在该函数中,数据通过 jsonDecode 函数转换为字符串。我的代码返回码在运行时返回错误type 'Future' is not a subtype of type 'String'

如何在我的代码中返回变量 graphResponse,这是一个 Future 并将其传递给 jsonExtractor 函数。下面

class FetchFB {

  final String url;
  final String token;

  FetchFB({@required this.url, @required this.token});

  Future<http.Response> fetchData() async {
    http.Response graphResponse = await http.get('$url$token');
    print(graphResponse);
    return graphResponse;
  }

  dynamic jsonExtractor(dynamic dataResponse) async {
    return await jsonDecode(dataResponse);
  }
}

在 main.dart 中

    String url = 'https://graph.facebook.com/v7.0/me?fields=';
        String tok = '0BTkTGrLtBgsp35WQZDZD';
        
        class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      final bool _loggedIn = false;
      final fbLogin = FacebookLogin();
      
    
      void flogin() async {
        final result = await fbLogin.logIn(['email']);
        final token = result.accessToken.token; // Not used for the the time being
    
        FetchFB data = FetchFB(url: url, token: tok);
        var fb = data.fetchData();
        var fb1 = data.jsonExtractor(fb);
        print(fb1);
      }

@override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Center(
              child: OutlineButton(
                onPressed: () {
                  flogin();
                },
                child: Text('FaceBook'),
              ),
            )
          ],
        ),
      ),
    );
  }
}

【问题讨论】:

  • 更新您的问题,正确解释您想要实现的目标以及您从哪里获得异常?
  • @ZeeshanHussain 我编辑了,请再看看。谢谢
  • 你仍然没有显示这个调用是在哪里完成的
  • 一旦你显示你在哪里调用 fetchData() 函数,我会更新我的答案。
  • @ZeeshanHussain 我已经更新了主题。请看一看。

标签: android flutter dart


【解决方案1】:

您需要等待未来完成。同样要解码 JSON,您需要从 response.body 中获取 JSON。

    String url = 'https://graph.facebook.com/v7.0/me?fields=';
        String tok = '0BTkTGrLtBgsp35WQZDZD';
        
        class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      final bool _loggedIn = false;
      final fbLogin = FacebookLogin();
      
    
      void flogin() async {
        final result = await fbLogin.logIn(['email']);
        final token = result.accessToken.token; 
   
        FetchFB data = FetchFB(url: url, token: tok);
        var fb = await data.fetchData(); // wait for the future to complete.
        var fb1 = data.jsonExtractor(fb);
        print(fb1);
      }

@override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Center(
              child: OutlineButton(
                onPressed: () {
                  flogin();
                },
                child: Text('FaceBook'),
              ),
            )
          ],
        ),
      ),
class FetchFB {

  final String url;
  final String token;

  FetchFB({@required this.url, @required this.token});

  Future<http.Response> fetchData() async {
    http.Response graphResponse = await http.get('$url$token');
    print(graphResponse);
    return graphResponse;
  }

  dynamic jsonExtractor(dynamic dataResponse) async {
    return jsonDecode(dataResponse.body);
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 2019-11-10
    • 2021-09-19
    相关资源
    最近更新 更多