简体   繁体   English

Flutter http 请求下发

[英]Flutter http request issue

I'm trying to check an url with the http package.我正在尝试使用http package 检查 url。

The code looks like this:代码如下所示:

  Future<void> checkURl() async{
    final response =
        await http.get(
        Uri.parse('https://someurl.com'));
  }

The http package is imported, but somehow the IDE marks the 'http' invalid. http package 已导入,但不知何故 IDE 将“http”标记为无效。 This is the error message:这是错误消息:

Error: The getter 'http' isn't defined for the class '_MyHomePageState'.
 - '_MyHomePageState' is from 'package:movies_app/screens/detail_screen.dart' ('lib/screens/detail_screen.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'http'.
    final response = await http.get(Uri.parse('https://someurl'));

How could I solve this?我该如何解决这个问题?

As for the use case, you are using as prefix to import http, import like至于用例,您使用 as 前缀来导入 http,像这样导入

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

To use like await http.getawait http.get一样使用

no need to do http.get , you can do不需要做http.get ,你可以做

 import 'package:http/http.dart';

 Future<void> checkURl() async{
    final response =
        await get(Uri.parse('https://someurl.com'));
  }

if you want to do如果你想做

await http.get(Uri.parse('https://someurl.com'));

then import the package as然后将 package 导入为

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


Future<void> yourUrlFunction() async{
     final response =await get(Uri.parse('https://someurl.com'));
}

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

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