Undefined name 'http' in FLUTTER even after restarting the IDE

4,086

Change import 'package:http/http.dart' as http; instead of import 'package:http/http.dart'; in your dart code.

Share:
4,086
Aashiq
Author by

Aashiq

Myself a very passionate on web-development and Javascript Frameworks,mostly React.Loves to work on cool projects and build great things.Learning through mistakes is my favourite job.

Updated on December 22, 2022

Comments

  • Aashiq
    Aashiq over 1 year

    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!

  • Aashiq
    Aashiq almost 4 years
    Hey @Nikunj,it really saved me,you owe me a long time,Hope you can also save me in future 😉