'Issues with Easy Geofencing in Flutter when i am trying to call the service more then one time

i am trying to check if the user in specific geofencing zone using the Easy geofencing package but every time i call the EasyGeofencing.startGeofenceService() and EasyGeofencing.getGeofenceStream() with new position ( pointedLatitude, pointedLongitude, radiusMeter) the service do not work and the console print "Parse value===> false" if any one know how i solve this problem please help me, i am stuck since a week.

this is my code

import 'dart:async';
import 'package:easy_geofencing/easy_geofencing.dart';

import 'package:geolocator/geolocator.dart';
import 'package:easy_geofencing/enums/geofence_status.dart';
import 'package:flutter/material.dart';
import 'package:flutter_application_1/domain/models/shop_model.dart';
import 'package:flutter_application_1/presentation/resources/assets_manager.dart';
import 'package:flutter_application_1/presentation/resources/color_manager.dart';
import 'package:flutter_application_1/presentation/resources/values_manager.dart';
import 'package:flutter_application_1/presentation/sidebars/cardWidget.dart';
import 'package:flutter_application_1/presentation/sidebars/profileSideBar.dart';
import 'package:flutter_application_1/services/shop_services.dart';
import 'package:provider/provider.dart';
import 'package:rxdart/rxdart.dart';

class ShopsDownBar extends StatefulWidget with ChangeNotifier {
  ShopsDownBar({Key? key}) : super(key: key);

  @override
  State<ShopsDownBar> createState() => _ShopsDownBarState();
}

class _ShopsDownBarState extends State<ShopsDownBar>
    with SingleTickerProviderStateMixin<ShopsDownBar> {
  StreamSubscription<GeofenceStatus>? geofenceStatusStream;
  StreamSubscription<EasyGeofencing>? easyGeofencingStream;
  Geolocator geolocator = Geolocator();
  String geofenceStatus = '';
  bool isReady = false;
  int raduis = 25;
  double? longitude;
  double? latitude;
  Position? position;
  late StreamController<bool> isOpenStreamController;
  late Stream<bool> isOpenNStream;
  late StreamSink<bool> isOpenNSink;
  late AnimationController _animationControler;
  List<Shops> shops = [];

  getCurrentPosition() async {
    position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);
    print("LOCATION => ${position?.toJson()}");
    isReady = (position != null) ? true : false;
  }

  setLocation() async {
    await getCurrentPosition();
    print("POSITION => ${position!.toJson()}");
  }

  final _animationDuration = const Duration(milliseconds: 500);
  setShops() {
    ShopServices().fetchShops().then((value) {
      setState(() {
        if (value != null) {
          for (int i = 0; i < value.length; i++) {
            shops.add(Shops(
              type: value[i].type,
              shopStatus: value[i].shopStatus,
              rewards: value[i].rewards,
              id: value[i].id,
              shopName: value[i].shopName,
              shopAddress: value[i].shopAddress,
              markerShop: value[i].markerShop,
              createdAt: value[i].createdAt,
              updatedAt: value[i].updatedAt,
            ));
          }
        }
      });
    });
  }

  void onIconPressed() {
    final animationStatus = _animationControler.status;
    final isAnimationDone = animationStatus == AnimationStatus.completed;
    if (isAnimationDone) {
      isOpenNSink.add(false);
      _animationControler.reverse();
    } else if (Provider.of<ProfileSideBar>(context, listen: false).isOpen ==
        false) {
      isOpenNSink.add(true);
      _animationControler.forward();
    }
  }

  @override
  void initState() {
    setShops();
    getCurrentPosition();
    if (isReady) {
      print('jawna behi');
      setState(() {
        setLocation();
      });
    }

    _animationControler =
        AnimationController(vsync: this, duration: _animationDuration);
    isOpenStreamController = PublishSubject<bool>();

    isOpenNStream = isOpenStreamController.stream;
    isOpenNSink = isOpenStreamController.sink;
    super.initState();
  }

  @override
  void dispose() {
    _animationControler.dispose();
    isOpenStreamController.close();
    isOpenNSink.close();
    EasyGeofencing.stopGeofenceService();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    var size = MediaQuery.of(context).size;
    var height = size.height;
    var width = size.width;

    return StreamBuilder<bool>(
        initialData: false,
        stream: isOpenNStream,
        builder: (context, isOpenAsync) {
          return AnimatedPositioned(
            duration: _animationDuration,
            top: isOpenAsync.data == false ? height * 0.91 : height * 0.24,
            bottom: AppSize.s1_5,
            right: AppSize.s1_5,
            left: AppSize.s1_5,
            child: Column(
              children: [
                Align(
                  child: GestureDetector(
                    onTap: () {
                      onIconPressed();
                    },
                    child: Container(
                      //  alignment: Alignment.cen,
                      padding: const EdgeInsets.only(
                        left: AppMargin.m60,
                        right: AppMargin.m60,
                        top: AppMargin.m8,
                      ),
                      child: Icon(
                        isOpenAsync.data == true
                            ? Icons.close
                            : Icons.wallet_giftcard,
                        size: AppSize.s28,
                        color: ColorManager.primary,
                      ),
                      decoration: const BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.only(
                            topLeft: Radius.circular(AppSize.s40),
                            topRight: Radius.circular(AppSize.s40)),
                      ),
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                      margin: const EdgeInsets.only(
                          left: AppMargin.m16, right: AppMargin.m16),
                      height: height / 1.4,
                      width: width,
                      decoration: BoxDecoration(
                        color: ColorManager.white,
                        borderRadius: const BorderRadius.only(
                            topLeft: Radius.circular(AppSize.s28),
                            topRight: Radius.circular(AppSize.s28)),
                      ),
                      child: shops.isNotEmpty
                          ? ListView.builder(
                              itemBuilder: (context, index) {
                                return Padding(
                                  padding: const EdgeInsets.only(
                                      bottom: AppPadding.p8,
                                      top: AppPadding.p18),
                                  child: Giftcart(
                                    ontap: () {
                                      // print("starting geoFencing Service");
                                      EasyGeofencing.startGeofenceService(
                                          pointedLatitude: shops[index]
                                              .markerShop
                                              .locations
                                              .latitude
                                              .toString(),
                                          pointedLongitude: shops[index]
                                              .markerShop
                                              .locations
                                              .longitude
                                              .toString(),
                                          radiusMeter: raduis.toString(),
                                          eventPeriodInSeconds: 5);

                                      geofenceStatusStream ??=
                                          EasyGeofencing.getGeofenceStream()!
                                              .listen((GeofenceStatus? status) {
                                        print(status.toString());
                                        setState(() {
                                          geofenceStatus = status.toString();
                                        });
                                        if (status.toString() ==
                                            'GeofenceStatus.enter') {
                                          print("entered");
                                         
                                        } else {
                                          print("not entered");
                                        }
                                      });
                                    },
                                    shopName: shops[index].shopName,
                                    details: shops[index].shopAddress,
                                    imagePath: ImageAssets.logo1,
                                  ),
                                );
                              },
                              itemCount: shops.length)
                          : Padding(
                              padding: const EdgeInsets.fromLTRB(
                                  AppPadding.p100,
                                  AppPadding.p100,
                                  AppPadding.p100,
                                  AppPadding.p200),
                              child: CircularProgressIndicator(
                                valueColor: AlwaysStoppedAnimation<Color>(
                                    ColorManager.primary),
                              ),
                            )),
                ),
              ],
            ),
          );
        });
  }
}

giftCart widget

import 'package:flutter/material.dart';
import 'package:flutter_application_1/presentation/resources/color_manager.dart';
import 'package:flutter_application_1/presentation/resources/values_manager.dart';
//import 'package:flutter_application_1/presentation/resources/font_manager.dart';

class Giftcart extends StatelessWidget {
  final String shopName;
  final String details;
  final String imagePath;

  final void Function() ontap;

  const Giftcart({
    Key? key,
    required this.ontap,
    required this.shopName,
    required this.details,
    required this.imagePath,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var size = MediaQuery.of(context).size;
    var height = size.height;
    var width = size.width;
    return Center(
      child: Container(
        decoration: BoxDecoration(
          color: ColorManager.white,
          borderRadius: BorderRadius.circular(10),
          boxShadow: [
            BoxShadow(
              color: ColorManager.grey,
              offset: const Offset(0, 0),
              blurRadius: 10,
            ),
          ],
        ),
        height: height * 0.1,
        width: width * 0.8,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Container(
                height: height * 0.15,
                width: width * 0.15,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(40),
                ),
                child: Image.asset(imagePath)),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(
                  height: height * 0.02,
                ),
                Text(
                  shopName,
                  style: Theme.of(context).textTheme.subtitle2,
                  textAlign: TextAlign.end,
                ),
                SizedBox(
                  height: height * 0.01,
                ),
                Text(
                  details,
                  style: Theme.of(context).textTheme.bodyText1,
                  textAlign: TextAlign.start,
                ),
              ],
            ),
            Padding(
              padding: const EdgeInsets.only(right: 10.0),
              child: SizedBox(
                height: height * 0.12,
                width: width * 0.12,
                child: FloatingActionButton(
                    backgroundColor: ColorManager.primary,
                    onPressed: ontap,
                    child: Icon(
                      Icons.card_giftcard,
                      size: AppSize.s18,
                      color: ColorManager.white,
                    )),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

the Screen the button on the left is for checking the geofenceStatus

enter image description here

this is the package doc



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source