'Flutter weather api shows error type 'Null' is not a subtype of type'Weather'
main.dart
import 'package:flutter/material.dart';
import 'package:flutter_project/currentWeather.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: CurrentWeatherPage(),
);
}
}
models/weather.dart
class Weather{
final double temp;
final double feelsLike;
final double low;
final double high;
final String description;
Weather({ required this.temp, required this.feelsLike, required this.low, required this.high, required this.description});
factory Weather.fromJson(Map<String,dynamic>json){
return Weather(
temp: json['main']['temp'].toDouble(),
feelsLike: json['main']['feels_like'].toDouble(),
low: json['main']['temp_min'].toDouble(),
high: json['main']['temp_max'].toDouble(),
description: json['weather'][0]['description'],
);
}
}
currentWeather.dart
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'models/weather.dart';
class CurrentWeatherPage extends StatefulWidget {
const CurrentWeatherPage({ Key? key }) : super(key: key);
@override
State<CurrentWeatherPage> createState() => _CurrentWeatherPageState();
}
class _CurrentWeatherPageState extends State<CurrentWeatherPage> {
late final Weather _weather;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: FutureBuilder(
builder: (context, AsyncSnapshot snapshot) {
// ignore: unnecessary_null_comparison
if (snapshot != null) {
Weather _weather = snapshot.data;
// ignore: unnecessary_null_comparison
if (_weather == null) {
return const Text("Error getting weather");
} else {
return weatherBox(_weather);
}
} else {
return const CircularProgressIndicator();
}
},
future: getCurrentWeather(),
),
)
);
}
}
Widget weatherBox(Weather _weather) {
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
margin: const EdgeInsets.all(10.0),
child:
Text("${_weather.temp}°C",
textAlign: TextAlign.center,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 55),
)
),
Container(
margin: const EdgeInsets.all(5.0),
child: Text("${_weather.description}")
),
Container(
margin: const EdgeInsets.all(5.0),
child: Text("Feels:${_weather.feelsLike}°C")
),
Container(
margin: const EdgeInsets.all(5.0),
child: Text("H:${_weather.high}°C L:${_weather.low}°C")
),
]
);
}
Future getCurrentWeather() async {
late Weather weather;
String city = "karak,my";
String apiKey = "saasfdglkoqn";
var url = Uri.parse("https://api.openweathermap.org/data/2.5/weather?q=$city&appid=$apiKey&units=metric");
final response = await http.get(url);
if (response.statusCode == 200) {
weather = Weather.fromJson(jsonDecode(response.body));
}
return weather;
}
Received this error during run
Exception caught by widgets library
type 'Null' is not a subtype of type 'Weather'
The relevant error-causing widget was
FutureBuilder dynamic
how can i solve this issue? Can i know the reason behind this error. I have been trying to resolve the issue for some time please help me to sort it out. Thanks in advance
Solution 1:[1]
API requires key.
{
cod: 401,
message: "Invalid API key. Please see http://openweathermap.org/faq#error401 for more info."
}
Throw an exception when API returns not OK.
Future<Weather> getCurrentWeather() async {
late Weather weather;
String city = "karak,my";
String apiKey = "saasfdglkoqn";
var url = Uri.parse("https://api.openweathermap.org/data/2.5/weather?q=$city&appid=$apiKey&units=metric");
final response = await http.get(url);
if (response.statusCode == 200) {
weather = Weather.fromJson(jsonDecode(response.body));
} else {
throw Exception(response.body);
}
return weather;
}
AsyncSnapshot has properties hasData and hasError to check a state.
body: Center(
child: FutureBuilder(
builder: (context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
return weatherBox(snapshot.data);
} else if (snapshot.hasError) {
return Text(snapshot.error.toString());
} else {
return const CircularProgressIndicator();
}
},
future: getCurrentWeather(),
),
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user18309290 |
