2

This code is working fine in emulator but it doesn't work properly in real device . This code for taking user location but when i open 1st time app it only ask permission for location and doesn't getlocation if restart my application then it take location.

@override
  void initState() {
    // TODO: implement initState
    super.initState();
    showAddress(context);
    didChangeDependencies();
  }

  Future showAddress(BuildContext context) async {
    await locationService();
  }

  void getYourAddress() {
    homeServices.addAddress(
      context: context,
      address: address,
      latitude: lat!,
      logitude: long!,
      locality: locality,
    );
  }

  double? lat;

  double? long;

  String address = '';
  String locality = '';

//For convert lat long to address
  getAddress(lat, long) async {
    List<geocoding.Placemark> placemarks =
        await geocoding.placemarkFromCoordinates(lat, long);
    if (mounted) {
      setState(() {
        address =
            "${placemarks[0].street!} ${placemarks[0].subLocality!} ${placemarks[0].locality!} ${placemarks[0].postalCode!} ${placemarks[0].country!} ";
        if (placemarks[0].locality == '') {
          locality = placemarks[0].street!;
        } else {
          locality = placemarks[0].locality!;
        }
      });
    }

    for (int i = 0; i < placemarks.length; i++) {
      print("INDEX $i ${placemarks[i]}");
    }
    getYourAddress();
  }

  Future locationService() async {
    Location location = Location();

    bool serviceEnabled;
    PermissionStatus permissionLocation;
    LocationData locData;

    serviceEnabled = await location.serviceEnabled();
    if (serviceEnabled == false) {
      serviceEnabled = await location.requestService();
      if (serviceEnabled == true) {
        permissionLocation = await location.hasPermission();
        if (permissionLocation == PermissionStatus.denied) {
          permissionLocation = await location.requestPermission();
          if (permissionLocation != PermissionStatus.granted) {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (_) => const IntroScreen(),
              ),
            );
          } else if (permissionLocation == PermissionStatus.granted) {
            await getLocation();
          }
        } else if (permissionLocation == PermissionStatus.granted) {
          await getLocation();
        }
      }
    } else if (serviceEnabled == true) {
      permissionLocation = await location.hasPermission();
      if (permissionLocation == PermissionStatus.denied) {
        permissionLocation = await location.requestPermission();
        if (permissionLocation != PermissionStatus.granted) {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (_) => const IntroScreen(),
            ),
          );
        } else if (permissionLocation == PermissionStatus.granted) {
          await getLocation();
        }
      } else if (permissionLocation == PermissionStatus.granted) {
        await getLocation();
      }
    } else {
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (_) => const IntroScreen(),
        ),
      );
    }
  }


// Problem occuring at this point in emulator it work fine but not work in real device.
  getLocation() async {
    try {
      LocationData locData;
      Location location = Location();

      locData = await location.getLocation();

      if (mounted) {
        setState(() {
          lat = locData.latitude!;
          long = locData.longitude!;
        });
        await getAddress(lat, long);
      }
    } catch (e) {
      getLocation();
    }
  }

This code is working fine in emulator but it doesn't work properly in real device . This code for taking user location but when i open 1st time app it only ask permission for location and doesn't getlocation if restart my application then it take location. I am expecting this app to ask user for location permission and store data imidiately to database. But it doesn't doing it on first time after i restart app then it take location . Please help me to solve this problem. I am use Using Location and Gecoding widget of flutter. when i try ti= check logs of my app in real device it show that asynchronous suspension.

Luffy
  • 23
  • 3

0 Answers0