Home > Mobile >  Flutter http request issue
Flutter http request issue

Time:08-16

I'm trying to check an url with the http package.

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. 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?

CodePudding user response:

As for the use case, you are using as prefix to import http, import like

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

To use like await http.get

CodePudding user response:

no need to do http.get, you can do

 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

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