Ballazx
Ballazx

Reputation: 569

Flutter http request issue

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?

Upvotes: 1

Views: 1028

Answers (2)

Sujan Gainju
Sujan Gainju

Reputation: 4769

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;

Upvotes: 2

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63559

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

Upvotes: 3

Related Questions