Getting error for Flutter http.get('url') parameter It's not accepting Url String

Issue

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=/"

Solution

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

Answered By – S. M. JAHANGIR

Answer Checked By – Mary Flores (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *