im have some error:
I/flutter (30216): NoSuchMethodError: The method ‘[]’ was called on null.
I/flutter (30216): Receiver: null
I/flutter (30216): Tried calling:
There is my main.dart code:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_weather/models/additionalWeatherData.dart';
import 'package:flutter_weather/models/geocode.dart';
import 'package:geolocator/geolocator.dart';
import 'package:http/http.dart' as http;
import 'package:latlong2/latlong.dart';
import '../models/dailyWeather.dart';
import '../models/hourlyWeather.dart';
import '../models/weather.dart';
class WeatherProvider with ChangeNotifier {
String apiKey = '744b94562476e3686d8944d72c0bb1f2';
late Weather weather;
late AdditionalWeatherData additionalWeatherData;
LatLng? currentLocation;
List<HourlyWeather> hourlyWeather = [];
List<DailyWeather> dailyWeather = [];
bool isLoading = false;
bool isRequestError = false;
bool isSearchError = false;
bool isLocationserviceEnabled = false;
LocationPermission? locationPermission;
bool isCelsius = true;
String get measurementUnit => isCelsius ? '°C' : '°F';
Future<Position?> requestLocation(BuildContext context) async {
isLocationserviceEnabled = await Geolocator.isLocationServiceEnabled();
notifyListeners();
if (!isLocationserviceEnabled) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Location service disabled')),
);
return Future.error('Location services are disabled.');
}
locationPermission = await Geolocator.checkPermission();
if (locationPermission == LocationPermission.denied) {
isLoading = false;
notifyListeners();
locationPermission = await Geolocator.requestPermission();
if (locationPermission == LocationPermission.denied) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Permission denied'),
));
return Future.error('Location permissions are denied');
}
}
if (locationPermission == LocationPermission.deniedForever) {
isLoading = false;
notifyListeners();
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
'Location permissions are permanently denied, Please enable manually from app settings',
),
));
return Future.error('Location permissions are permanently denied');
}
return await Geolocator.getCurrentPosition();
}
Future<void> getWeatherData(
BuildContext context, {
bool notify = false,
}) async {
isLoading = true;
isRequestError = false;
isSearchError = false;
if (notify) notifyListeners();
Position? locData = await requestLocation(context);
if (locData == null) {
isLoading = false;
notifyListeners();
return;
}
try {
currentLocation = LatLng(locData.latitude, locData.longitude);
await getCurrentWeather(currentLocation!);
await getDailyWeather(currentLocation!);
} catch (e) {
print(e);
isRequestError = true;
} finally {
isLoading = false;
notifyListeners();
}
}
Future<void> getCurrentWeather(LatLng location) async {
Uri url = Uri.parse(
'https://api.openweathermap.org/data/2.5/weather?lat=${location.latitude}&lon=${location.longitude}&units=metric&appid=$apiKey',
);
try {
final response = await http.get(url);
final extractedData = json.decode(response.body) as Map<String, dynamic>;
weather = Weather.fromJson(extractedData);
print('Fetched Weather for: ${weather.city}/${weather.countryCode}');
} catch (error) {
print(error);
isLoading = false;
this.isRequestError = true;
}
}
Future<void> getDailyWeather(LatLng location) async {
isLoading = true;
notifyListeners();
Uri dailyUrl = Uri.parse(
'https://api.openweathermap.org/data/2.5/onecall?lat=${location.latitude}&lon=${location.longitude}&units=metric&exclude=minutely,current&appid=$apiKey',
);
try {
final response = await http.get(dailyUrl);
final dailyData = json.decode(response.body) as Map<String, dynamic>;
additionalWeatherData = AdditionalWeatherData.fromJson(dailyData);
List dailyList = dailyData['daily'];
List hourlyList = dailyData['hourly'];
hourlyWeather = hourlyList
.map((item) => HourlyWeather.fromJson(item))
.toList()
.take(24)
.toList();
dailyWeather =
dailyList.map((item) => DailyWeather.fromDailyJson(item)).toList();
} catch (error) {
print(error);
isLoading = false;
this.isRequestError = true;
}
}
Future<GeocodeData?> locationToLatLng(String location) async {
try {
Uri url = Uri.parse(
'http://api.openweathermap.org/geo/1.0/direct?q=$location&limit=5&appid=$apiKey',
);
final http.Response response = await http.get(url);
if (response.statusCode != 200) return null;
return GeocodeData.fromJson(
jsonDecode(response.body)[0] as Map<String, dynamic>,
);
} catch (e) {
print(e);
return null;
}
}
Future<void> searchWeather(String location) async {
isLoading = true;
notifyListeners();
isRequestError = false;
print('search');
try {
GeocodeData? geocodeData;
geocodeData = await locationToLatLng(location);
if (geocodeData == null) throw Exception('Unable to Find Location');
await getCurrentWeather(geocodeData.latLng);
await getDailyWeather(geocodeData.latLng);
// replace location name with data from geocode
// because data from certain lat long might return local area name
weather.city = geocodeData.name;
} catch (e) {
print(e);
isSearchError = true;
} finally {
isLoading = false;
notifyListeners();
}
}
void switchTempUnit() {
isCelsius = !isCelsius;
notifyListeners();
}
}
I’ve try to find solution in web but i cant see nothing like that. I take the open-source project of this app from GitHub, and there’s nothing written about it.
New contributor
Stepan Volkov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.