flutter 波动http请求问题

2ic8powd  于 2023-01-21  发布在  Flutter
关注(0)|答案(2)|浏览(123)

我正在尝试使用http包检查url。
代码如下所示:

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

http包已导入,但IDE不知何故将"http"标记为无效。以下是错误消息:

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

我该怎么解决这个问题?

uxh89sit

uxh89sit1#

至于使用情形,您使用作为前缀导入http,导入如下

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

如使用await http.get

fdx2calv

fdx2calv2#

不需要执行http.get,您可以执行

import 'package:http/http.dart';

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

如果你想做

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

然后将包导入为

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

相关问题