user14836914
user14836914

Reputation:

Problem in making http.get() request in flutter

I am learning about API's and http request in flutter and I am facing problem in making a get request as in any tutorial they are directly pasting string URL inside get as parameter but when I post it as string it is showing error:

The argument type 'String' can't be assigned to the parameter type 'Uri'.

Can anyone help me in this? This is my sample code :

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

void main(List<String> arguments) async {
  // This example uses the Google Books API to search for books about http.
  // https://developers.google.com/books/docs/overview
  var url = 'https://www.googleapis.com/books/v1/volumes?q={http}';

  // Await the http get response, then decode the json-formatted response.
  var response = await http.get(url); // i am getting error here
  if (response.statusCode == 200) {
    var jsonResponse = convert.jsonDecode(response.body);
    var itemCount = jsonResponse['totalItems'];
    print('Number of books about http: $itemCount.');
  } else {
    print('Request failed with status: ${response.statusCode}.');
  }
}

Here is image of my code with error:

enter image description here

Upvotes: 6

Views: 40917

Answers (6)

Anubrat Sahoo
Anubrat Sahoo

Reputation: 21

Future getTeams() async {
    try {
      var response = await http.get(Uri.parse('http://localhost:3000'));
      print(response.body);
    } catch (e) {
      print('Caught error: $e');
    }
  }

just replace the localhost keyword with 10.0.2.2. The final response should look like this http://10.0.2.2:3000. ps - i have only tested it with a Android emulator

Upvotes: 0

IBRAHIM ALI MUSAH
IBRAHIM ALI MUSAH

Reputation: 979

First, add the http library to your project:

flutter pub add http

Then import http as http:

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

Then parse your link to Uri using:

var url = Uri.parse('https://www.googleapis.com/books/v1/volumes?q={http}');
http.Response response = await http.get(url);
try {
  if (response.statusCode == 200) {
    String data = response.body;
    var decodedData = jsonDecode(data);
    return decodedData;
  } else {
    return 'failed';
  }
} catch (e) {
  return 'failed';
}

Upvotes: 17

Prantik Paul
Prantik Paul

Reputation: 51

If still doesn't work try this:

import 'package:http/http.dart';

var response = get(Uri.parse('https://www.google.com'));

Upvotes: 5

Vignesh
Vignesh

Reputation: 31

Try this (Add http to pubspec.yaml under dependencies):

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

var response = http.get(Uri.parse('https://www.google.com'));

Upvotes: 2

yahya007
yahya007

Reputation: 168

You passing string the error says need an uri so create an uri and use in it.

var uri = new Uri.http("example.org", "/path", { "q" : "{http}" });

Upvotes: 1

matr0s
matr0s

Reputation: 107

First of all, check your pubspec.yaml file and HTTP version. It should be the actual one you can find here: https://pub.dev/packages/http/install For example, it is:

http: ^0.12.2 

at the moment

Here is my code and it works well:

main.dart

import 'package:flutter/material.dart';
import 'package:stackowerflow/my_app.dart';

void main() {
  runApp(MyApp());
}

my_app.dart

import 'dart:convert' as convert;
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class MyApp extends StatelessWidget {
  Future<void> stackHelp() async {
    var url = 'https://www.googleapis.com/books/v1/volumes?q={http}';

    // Await the http get response, then decode the json-formatted response.
    var response = await http.get(url);
    if (response.statusCode == 200) {
      var jsonResponse = convert.jsonDecode(response.body);
      var itemCount = jsonResponse['totalItems'];
      print('Number of books about http: $itemCount.');
    } else {
      print('Request failed with status: ${response.statusCode}.');
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Flutter WebView '),
        ),
        body: Container(
          child: TextButton(onPressed: stackHelp, child: Text('Push me')),
        ),
      ),
    );
  }
}

RESULT

flutter: Number of books about http: 485.

Upvotes: 0

Related Questions