dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.26k stars 1.58k forks source link

LateError (LateInitializationError: Field 'latitude' has not been initialized.) #46270

Closed themak01 closed 3 years ago

themak01 commented 3 years ago

Getting this error after when i updated my android studio and emulator initially it was working

CODE

import 'package:geolocator/geolocator.dart';

class Location {
  late double latitude;
  late 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);
    }
  }
}

//Main.dart

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:weather_app/services/location.dart';
import 'package:http/http.dart' as http;

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

class _LoadingScreenState extends State<LoadingScreen> {
  @override
  void initState() {
    super.initState();
    getLocation();
  }

  getLocation() async {
    Location location = Location();
    await location.getCurrentLocation();

    print(location.latitude);
    print(location.longitude);
  }

  void getData() async {
    http.Response response = await http.get(Uri.parse(
        "http://api.openweathermap.org/data/2.5/weather?q=London&appid=bd247a015f492dc90288c1bb3d60c27e"));

    if(response.statusCode == 200){
      String data = response.body;

      var longitude = jsonDecode(data)['coord']['lon'];
      print(longitude);
    }else{
      print(response.statusCode);
    }
  }

  @override
  Widget build(BuildContext context) {
    getData();
    return Scaffold();
  }
}

flutter lateError

devoncarew commented 3 years ago

Thanks for the report; @themak01 - can you re-file this at flutter/flutter?

SantosVision commented 2 years ago

soo, this issue was closed with no solutions?