9

im new to flutter,i tried to use http dependency in my App,The steps are followed as,

1.imported in pubspec.yaml file

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.3
  http: ^0.12.1

2)Then i run the, >flutter pub get,command,

  1. Then i imported the package in my dart code as ,
import 'package:http/http.dart';

class Homepage extends StatefulWidget {
  @override
  _HomepageState createState() => _HomepageState();
}

class _HomepageState extends State<Homepage> {
  var url = "http://jsonplaceholder.typicode.com/photos";

  // Used to initialize something before starting of the screen
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    fetchdata();
  }

  fetchdata() async {
    var res = await http.get(url);
    print(res);
  }

As you can see from the below code,I want to get the response from the URL and want to print the response,i even restarted my IDE after this several times,but it didnt show up,the error message is ,

compiler message: lib/pages/home_page.dart:22:21: Error: The getter 'http' isn't defined for the class '_HomepageState'.

  • '_HomepageState' is from 'package:first_app/pages/home_page.dart' ('lib/pages/home_page.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'http'. var res = await http.get(url);

This is just a simple error i know,but it'll be a great help for me,Thanks in advance!

2 Answers 2

39

Change

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

instead of

import 'package:http/http.dart';

in your dart code.

Sign up to request clarification or add additional context in comments.

Comments

0
  1. Paste the below code in pubspec.yaml
    dependencies:
      http: ^0.12.0
    flutter:
      sdk: flutter
    
  2. Run
    flutter pub get
    
  3. So it will download all the lib files

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.