ArizArmeidi / FlutterWeather

Weather app created using Flutter and Dart
MIT License
302 stars 96 forks source link

request error, please check your internet connection #11

Open imacro1212 opened 4 months ago

imacro1212 commented 4 months ago

Thanks, but the app shows request error, please check your internet connection page. I added my working api key and checked internet permission and the problem still exist. Thanks again.

ArizArmeidi commented 4 months ago

are there any errors in the debug log?

gwabstech commented 3 months ago

same issue no log related

vnl commented 1 month ago

Same error: If it helps these are the console logs:

I/flutter ( 4481): Fetched Weather for: Mountain View/US
I/flutter ( 4481): NoSuchMethodError: The method '[]' was called on null.
I/flutter ( 4481): Receiver: null
I/flutter ( 4481): Tried calling: [](0)
vnl commented 1 month ago

Ah.. I think I figured this out. The OneCall API needs a different API key, which needs billing enabled.

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',
    );

A minor change to add API checks would sort this out. Some quick bad coding.. but here it is:

       ...      
      final response = await http.get(dailyUrl);
      if (response.statusCode == 401) {
        print('Authentication error: 401 Unauthorized');
        isLoading = false;
        isRequestError = true;
        return;
      }
      final dailyData = json.decode(response.body) as Map<String, dynamic>;
      ....

OR

Just subscribe to OneCall 3.0: https://openweathermap.org/api/one-call-3 and give it some time to register and then change the URL to something like this:

Uri dailyUrl = Uri.parse(
      'https://api.openweathermap.org/data/3.0/onecall?lat=${location.latitude}&lon=${location.longitude}&units=metric&exclude=minutely,current&appid=$apiKey',
    );
ArizArmeidi commented 1 month ago

Ah.. I think I figured this out. The OneCall API needs a different API key, which needs billing enabled.

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',
    );

A minor change to add API checks would sort this out. Some quick bad coding.. but here it is:

       ...      
      final response = await http.get(dailyUrl);
      if (response.statusCode == 401) {
        print('Authentication error: 401 Unauthorized');
        isLoading = false;
        isRequestError = true;
        return;
      }
      final dailyData = json.decode(response.body) as Map<String, dynamic>;
      ....

OR

Just subscribe to OneCall 3.0: https://openweathermap.org/api/one-call-3 and give it some time to register and then change the URL to something like this:

Uri dailyUrl = Uri.parse(
      'https://api.openweathermap.org/data/3.0/onecall?lat=${location.latitude}&lon=${location.longitude}&units=metric&exclude=minutely,current&appid=$apiKey',
    );

Hmmm that's weird, my billing is turned off and I'm using the OneCall version 2.5 as stated in the readme. I will double check again it could be a regional thing. in the meantime when I have the time I will try to update the code to handle it