Home > Mobile >  API Key not working in Flutter Weather API App (Android)
API Key not working in Flutter Weather API App (Android)

Time:09-30

I'm learning Flutter and trying to build an Android Application. Basically a weather app where it fetches the API Key from a site. I keep getting the error " The argument type 'String' can't be assigned to the parameter type 'Uri'. ". What does this even mean and how do I get this to work?

main.dart

import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

void main() => runApp(
  MaterialApp(
    title: "Weather App",
    home: Home(),
  )
);

class Home extends StatefulWidget {
  @override
  State<StatefulWidget> createState(){
    return _HomeState();
  }
}

class _HomeState extends State<Home> {

  var temp;
  var description;
  var currently;
  var humidity;
  var windSpeed;

  Future getWeather () async {
    http.Response response = await http.get("http://api.weatherapi.com/v1/current.json?key=e5bd00e528e346ff8a840254213009&q=Chatham Ontario&aqi=no");
    var results = jsonDecode(response.body);
    setState((){
      this.temp = results['current']['temp_c'];
      this.description = results['current'][0]['last_updated'];
      this.currently = results['current'][0]['condition']['text'];
      this.humidity = results['current']['humidity'];
      this.windSpeed = results['current']['wind_kph'];
    });
  }

  @override
  void initState() {
    super.initState();
    this.getWeather();
  }

  @override
  Widget build (BuildContext context) {
    return Scaffold(
      body: Column(
        children: <Widget>[
          Container(
            height: MediaQuery.of(context).size.height / 3,
            width: MediaQuery.of(context).size.width,
            color: Colors.red,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Padding(
                  padding:EdgeInsets.only(bottom: 10.0),
                  child: Text(
                    "Currently in Chatham-Kent",
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 14.0,
                      fontWeight: FontWeight.w600
                    ),
                  ),
                ),
                Text(
                  temp != null ? temp.toString()   "\u00B0" : "Loading",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 40.0,
                    fontWeight: FontWeight.w600
                  ),
                ),
                Padding(
                  padding:EdgeInsets.only(top: 10.0),
                  child: Text(
                    currently != null ? currently.toString() : "Loading",
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 14.0,
                        fontWeight: FontWeight.w600
                    ),
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            child: Padding(
              padding: EdgeInsets.all(20.0),
              child: ListView(
                children: <Widget>[
                  ListTile(
                    leading: FaIcon(FontAwesomeIcons.thermometerHalf),
                    title: Text("Temperature"),
                    trailing: Text(temp != null ? temp.toString()   "\u00B0" : "Loading"),
                  ),
                  ListTile(
                    leading: FaIcon(FontAwesomeIcons.cloud),
                    title: Text("Weather"),
                    trailing: Text(description != null ? description.toString() : "Loading"),
                  ),
                  ListTile(
                    leading: FaIcon(FontAwesomeIcons.sun),
                    title: Text("Humidity"),
                    trailing: Text(humidity != null ? humidity.toString() : "Loading"),
                  ),
                  ListTile(
                    leading: FaIcon(FontAwesomeIcons.wind),
                    title: Text("Wind Speed"),
                    trailing: Text(windSpeed != null ? windSpeed.toString() : "Loading"),
                  )
                ],
              )
            )
          )
        ],
      ),
    );
  }
}

pubspec.yaml

version: 1.0.0 1

environment:
  sdk: '>=2.10.0 <3.0.0'

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.2
  font_awesome_flutter: ^8.0.0
  http: ^0.13.3

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^1.0.0

flutter:
  uses-material-design: true

CodePudding user response:

Well, Your error clearly saying The argument type 'String' can't be assigned to the parameter type 'Uri' so, you have to convert url string to Uri.

LIKE THIS:

var uri = Uri.parse("http://api.weatherapi.com/v1/current.json?key=e5bd00e528e346ff8a840254213009&q=Chatham Ontario&aqi=no");
 http.Response response = await http.get(uri);

CodePudding user response:

based on HTTP package docs here.

as of 0.13.0-nullsafety.0 version All APIs which previously allowed a String or Uri to be passed now require a Uri. so what happening here is that you need to parse(convert) your string to uri first before the call.

so your weather function will be like this

Future getWeather () async {
http.Response response = await http.get(Uri.parse("http://api.weatherapi.com/v1/current.json?key=e5bd00e528e346ff8a840254213009&q=Chatham Ontario&aqi=no"));
var results = jsonDecode(response.body);
setState((){
  this.temp = results['current']['temp_c'];
  this.description = results['current'][0]['last_updated'];
  this.currently = results['current'][0]['condition']['text'];
  this.humidity = results['current']['humidity'];
  this.windSpeed = results['current']['wind_kph'];
});

}

happy coding!

CodePudding user response:

Change this line of code

http.Response response = await http.get("http://api.weatherapi.com/v1/current.json?key=e5bd00e528e346ff8a840254213009&q=Chatham Ontario&aqi=no");

to

http.Response response = await http.get(Uri.parse("http://api.weatherapi.com/v1/current.json?key=e5bd00e528e346ff8a840254213009&q=Chatham Ontario&aqi=no"));
  • Related