What is the problem I don't understand here ? Just started Flutter coding this weather app project from Flutter beginner course on Udemy

Viewed 43

It Was working fine at first but after this lecture it returns Null.

Error I am getting is :

Performing hot restart...

Syncing files to device moto g52...

Restarted application in 2,126ms.

I/flutter (13494): 404

I/flutter (13494): null

Even after trying a new key it still returns that.

Note : I am still midway through the course (which has really gotten old) so no one is actually answering the questions.

OK this is a long one

1) main.dart

import 'package:clima/screens/location_screen.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(),
    );
  }
}

2) location_screen.dart

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> {
  @override
  void initState() {
    super.initState();
    print(widget.locationWeather);
  }
@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>[
                  TextButton(
                    onPressed: () {},
                    child: Icon(
                      Icons.near_me,
                      size: 50.0,
                    ),
                  ),
                  TextButton(
                    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,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

3) loading_screen.dart

import 'package:clima/screens/location_screen.dart';
import 'package:flutter/material.dart';
import 'package:clima/services/location_handler.dart';
import 'package:clima/services/networking.dart';
import 'location_screen.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

const myAPI = '7c8b4449639ae2b6b5e3bfd67a0e350f';

class LoadingScreen extends StatefulWidget {
  @override
  _LoadingScreenState createState() => _LoadingScreenState();
}

class _LoadingScreenState extends State<LoadingScreen> {
  late double latitood;
  late double longitood;

  void initState() {
    super.initState();
    getLocationData();
  }

  void getLocationData() async {
    Locator loca = Locator();
    await loca.getCurrentLocation();
    latitood = loca.latitude;
    longitood = loca.longitude;

    NetworkHelper NetHelp = NetworkHelper(
        'https://api.openweathermap.org/data/2.5.weather?lat=$latitood&lon=$longitood&appid=$myAPI');
    var weatherDataFinal = await NetHelp.getData();

    Navigator.push(context, MaterialPageRoute(builder: (context) {
      return LocationScreen(
          locationWeather:
              weatherDataFinal); //creating a path to location_screen.dart
      //and passing/pushing weather data to location screen through a constructor.
    }));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: SpinKitDoubleBounce(
          color: Colors.white,
          size: 100,
        ),
      ),
    );
  }
}

4) networking.dart

import 'dart:convert'; //imported to convert/use JSON data into our project.
import 'package:http/http.dart' as http;
class NetworkHelper {
  NetworkHelper(this.url);
  final String url;
  Future getData() async {
    http.Response response = await http.get(Uri.parse(url));
    //Passing the 'url' which is a String, will be converted into URI using 'Uri.parse' method.
    if (response.statusCode == 200) {
      String data = response.body;
      var decodedData = jsonDecode(data);
      //decoding and putting data into decodedData variable of dynamic type.
      return decodedData;
    } else {
      print(response.statusCode);
      print('Something\'s wrong I can feel it.');
    }
  }
}

5) location_handler.dart

import 'package:geolocator/geolocator.dart';

class Locator {    

  late double latitude;
  late double longitude;

  Future<void> getCurrentLocation() async {
    try {
      Position posi = await Geolocator.getCurrentPosition(
          desiredAccuracy: LocationAccuracy.low);
      latitude = posi.latitude;
      longitude = posi.longitude;
    } on Exception catch (e) {
      print(e);
    }
  }
}

Dependencies:

geolocator: ^7.0.2

http: ^0.13.1

flutter_spinkit: ^5.1.0

1 Answers
Related