1

I'm trying to fetch all json data which https://www.thecocktaildb.com/api/json/v1/1/filter.php?c=Cocktail api returns, to use it in my flutter app

var api="https://www.thecocktaildb.com/api/json/v1/1/filter.php?c=Cocktail";
var res=await http.get(api);
drinks= jsonDecode(res.body)["drinks"];

but it gives red underline under api parameter in http.get(api) function call that "argument of type string cannot be converted to URI" How to Resolve this? I even tried using res=await http.get(Uri.https('www.thecocktaildb.com','/api/json/v1/1/filter.php?c=Cocktail'));

but got error "Error connecting to the service protocol: failed to connect to http://127.0.0.1:63573"/_fprblq_tiY=/"

Mansi Mandlik
  • 120
  • 11

2 Answers2

0

Instead of using Uri.https(), try using Uri.parse()

S. M. JAHANGIR
  • 4,324
  • 1
  • 10
  • 30
  • It Throws an error even for res=await http.get(Uri.parse(api)); "Error connecting to the service protocol: failed to connect to http://127.0.0.1:64971/ypwDk3yQ650=/" – Mansi Mandlik Apr 02 '21 at 19:32
  • Seems like it's not a code issue at all! https://stackoverflow.com/questions/53933251/flutter-occurs-error-connecting-to-the-service-protocol-httpexception-erro – S. M. JAHANGIR Apr 02 '21 at 19:43
  • yes sir it wasn't code issue, resolved it with help of answers present here https://stackoverflow.com/questions/53933251/flutter-occurs-error-connecting-to-the-service-protocol-httpexception-erro – Mansi Mandlik Apr 02 '21 at 19:50
  • Its strange but it worked after 1)I disconnected my laptop from wifi 2)Unplugged usb cable and phone from laptop again connected laptop to wifi 3)connecting phone to laptop and running app – Mansi Mandlik Apr 02 '21 at 19:51
  • but all this happened after passing Uri.Parse(api) to http.get(Uri.Parse(api)) thanks. – Mansi Mandlik Apr 02 '21 at 19:53
0

Add : http: ^0.13.4 to your pubspec.yaml , it will provide you with some simple methods to access apis. You cannot add ? directly because it will convert to 3%F I believe, same for other special characters.

var url = 'thecocktaildb.com';
var urlExtension = '/api/json/v1/1/filter.php';
final Map<String, String> queryParameters = <String, String>{
  'c': 'Cocktail',
};
final api = Uri.https(url, urlExtension, queryParameters);

//It should be something like this, now you can get
//the api response the way you were doing:

var res=await http.get(api);
drinks= jsonDecode(res.body)["drinks"];
Hygison Brandao
  • 590
  • 1
  • 4
  • 16