2

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)),
                    ),
                  )
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

1 Answer 1

2

try this

var uri = Uri.parse('https://www.metaweather.com/api/location/search/?query=san');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.