Hi I am working on the London Brewery clima app but I am getting the Tried calling:. I have already looked up other solutions to this problem but they didn't work.
This is my code:
MAIN.DART
import 'package:flutter/material.dart';
import 'package:clima/screens/loading_screen.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark(),
home: LoadingScreen(),
);
}
}
LOCATION SCREEN
import 'package:flutter/material.dart';
import 'package:clima/utilities/constants.dart';
class LocationScreen extends StatefulWidget {
LocationScreen({this.locationWeather});
final locationWeather;
@override
_LocationScreenState createState() => _LocationScreenState();
}
class _LocationScreenState extends State<LocationScreen> {
double temperature;
int condition;
String cityName;
@override
void initState() {
super.initState();
updateUI(widget.locationWeather);
}
void updateUI(dynamic weatherData) {
temperature = weatherData['main']['temp'];
condition = weatherData['weather'][0]['id'];
cityName = weatherData['name'];
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('images/location_background.jpg'),
fit: BoxFit.cover,
colorFilter: ColorFilter.mode(
Colors.white.withOpacity(0.8), BlendMode.dstATop),
),
),
constraints: BoxConstraints.expand(),
child: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
FlatButton(
onPressed: () {},
child: Icon(
Icons.near_me,
size: 50.0,
),
),
FlatButton(
onPressed: () {},
child: Icon(
Icons.location_city,
size: 50.0,
),
),
],
),
Padding(
padding: EdgeInsets.only(left: 15.0),
child: Row(
children: <Widget>[
Text(
'32°',
style: kTempTextStyle,
),
Text(
'☀️',
style: kConditionTextStyle,
),
],
),
),
Padding(
padding: EdgeInsets.only(right: 15.0),
child: Text(
"It's 🍦 time in San Francisco!",
textAlign: TextAlign.right,
style: kMessageTextStyle,
),
),
],
),
),
),
);
}
}
LOADING SCREEN
import 'package:flutter/material.dart';
import 'package:clima/services/location.dart';
import 'package:clima/services/networking.dart';
import 'location_screen.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
const apiKey = '6e0c9beb6379456cd88d1fc1cdcbe15c';
class LoadingScreen extends StatefulWidget {
@override
_LoadingScreenState createState() => _LoadingScreenState();
}
class _LoadingScreenState extends State<LoadingScreen> {
double latitude;
double longitude;
@override
void initState() {
super.initState();
getLocationData();
}
void getLocationData() async {
Location location = Location();
await location.getCurrentLocation();
latitude = location.longitude;
longitude = location.latitude;
NetworkHelper networkHelper = NetworkHelper(
'https://api.openweathermap.org/data/2.5/weather?lat=$latitude&lon=$longitude&appid=$apiKey&units=metric');
var weatherData = await networkHelper.getData();
Navigator.push(context, MaterialPageRoute(builder: (context) {
return LocationScreen(
locationWeather: weatherData,
);
}));
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SpinKitDoubleBounce(
color: Colors.white,
size: 100.0,
)),
);
}
}
LOCATION SCREEN
import 'package:geolocator/geolocator.dart';
class Location {
Location({this.latitude, this.longitude});
double latitude;
double longitude;
Future<void> getCurrentLocation() async {
try {
Position position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
latitude = position.latitude;
longitude = position.longitude;
} catch (e) {
print(e);
}
}
}
NETWORKING
import 'package:http/http.dart' as http;
import 'dart:convert';
class NetworkHelper {
NetworkHelper(this.url);
final String url;
Future getData() async {
http.Response response = await http.get(url);
if (response.statusCode == 200) {
String data = response.body;
return jsonDecode(data);
} else {
print(response.statusCode);
}
}
}
I have already tried the next on the networking
String data = response.body;
var output = jsonDecode(data);
return output;
} else {
print(response.statusCode);
but it didn't work.
Could anyone help me out with this?
Thanks to all Diego
Given the following JSON from the openweathermap
API call, notice that main
property is not a List
but a Map
.
{
"coord": {
"lon": 0,
"lat": 0
},
"weather": [
{
"id": 804,
"main": "Clouds",
"description": "overcast clouds",
"icon": "04n"
}
],
"base": "stations",
"main": {
"temp": 23.22,
"feels_like": 23.56,
"temp_min": 23.22,
"temp_max": 23.22,
"pressure": 1015,
"humidity": 75,
"sea_level": 1015,
"grnd_level": 1015
},
"visibility": 10000,
"wind": {
"speed": 4.73,
"deg": 161,
"gust": 5
},
"clouds": {
"all": 100
},
"dt": 1658528443,
"sys": {
"sunrise": 1658469763,
"sunset": 1658513388
},
"timezone": 0,
"id": 6295630,
"name": "Globe",
"cod": 200
}
But the updateUI
function is trying to access it as List
. The fix is pretty simple, just remove the [0]
from temperature = weatherData['main'][0]['temp'];
like below:
void updateUI(dynamic weatherData) {
temperature = weatherData['main']['temp']; // <- Fix here
condition = weatherData['weather'][0]['id'];
cityName = weatherData['name'];
}
Thanks all for your help. I went ahead with the project and with the same code just worked fine. I don't know what changed actually but it works now. Thanks all
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.