“Future<Response>”类型的值不能分配给“Response”类型的变量

时间:2021-04-02 04:13:49

标签: flutter dart

我收到此错误:A value of type 'Future<Response>' can't be assigned to a variable of type 'Response'. Try changing the type of the variable, or casting the right-hand type to 'Response'.

    import 'package:flutter/material.dart';
    import 'package:http/http.dart' as http;
    
    class loadding extends StatefulWidget {
      @override
      _loaddingState createState() => _loaddingState();
    }
    
    class _loaddingState extends State<loadding> {
      void gateData() async {
        var url =
            Uri.parse('http://worldtimeapi.org/api/timezone/Africa/Addis_Ababa');

//here is The Error occur at http.get(url), 

        http.Response response = http.get(url);

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

3 个答案:

答案 0 :(得分:0)

我自己解决的。当我在 HTTP.get 前面添加 await 关键字时,错误消失了。

喜欢这个 http.Response response = await http.get(url);

答案 1 :(得分:0)

我已经编辑了你的代码,所以请你可以试试这个代码。

  void gateData() async {
    var url =
        Uri.parse('http://worldtimeapi.org/api/timezone/Africa/Addis_Ababa');
  http.Response response =await http.get(url);

  }

您需要使用 asyncawait 未来类型。

答案 2 :(得分:0)

试试这个

 void gateData() async {
        var url =
            Uri.parse('http://worldtimeapi.org/api/timezone/Africa/Addis_Ababa');

//here is The Error occur at http.get(url), 

        http.Response response = await http.get(url);

      }