【问题标题】:A value of type 'Future<Response>' can't be assigned to a variable of type 'Response' [duplicate]'Future<Response>' 类型的值不能分配给'Response' 类型的变量 [重复]
【发布时间】:2021-04-02 04:13:49
【问题描述】:

我收到此错误:A value of type 'Future&lt;Response&gt;' 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();
      }
    }

【问题讨论】:

  • 你需要把await放在http.get之前

标签: flutter dart


【解决方案1】:

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

像这样 http.Response 响应 = 等待 http.get(url);

【讨论】:

    【解决方案2】:

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

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

    您需要使用asyncawait 未来类型。

    【讨论】:

      【解决方案3】:

      试试这个

       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);
      
            }
      
      

      【讨论】:

        猜你喜欢
        • 2021-01-02
        • 1970-01-01
        • 1970-01-01
        • 2021-08-29
        • 1970-01-01
        • 2023-01-20
        • 1970-01-01
        • 1970-01-01
        • 2020-12-13
        相关资源
        最近更新 更多