How to parse API url in flutter
1,554
try this
var uri = Uri.parse('https://www.metaweather.com/api/location/search/?query=san');
Author by
Minsaf
Updated on December 19, 2022Comments
-
Minsaf over 1 year
I'm new to flutter and wanted to create a weather app. When calling the API, there's an error. I have created a function named fetchSearch() to search location. I have stored the API URL in a string variable and when calling the API, I parse the search as a parameter using the function fetchSearch() function
error: The argument type 'String' can't be assigned to the parameter type 'Uri'.
Flutter Code.
class _HomeState extends State<Home> { int temperature = 0; String location = "New York"; int woeid = 2487956; String url = "https://www.metaweather.com/api/location/search/?query=san"; void fetchSearch(String input) async { var searchResult = await http.get(url+input); //here's the error var result = json.decode(searchResult.body)[0]; setState(() { location = result["title"]; woeid = result["woeid"]; }); } @override Widget build(BuildContext context) { return MaterialApp( home: Container( decoration: BoxDecoration( image: DecorationImage( image: AssetImage('images/clear.png'), fit: BoxFit.cover), ), child: Scaffold( backgroundColor: Colors.transparent, body: Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.center, children: [ Column( children: [ Center( child: Text( temperature.toString() + " C", style: TextStyle(color: Colors.white, fontSize: 60.0), ), ), Center( child: Text( location, style: TextStyle(color: Colors.white, fontSize: 40.0), ), ), ], ), Column( children: [ Container( width: 300, child: TextField( style: TextStyle(color: Colors.white, fontSize: 25.0), decoration: InputDecoration( hintText: "Search location", hintStyle: TextStyle(color: Colors.white, fontSize: 18.0), prefixIcon: Icon(Icons.search)), ), ) ], ), ], ), ), ), ); } }