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:

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';
}
If still doesn't work try this:
import 'package:http/http.dart';
var response = get(Uri.parse('https://www.google.com'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With