Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad state: Stream has already been listened to #70

Closed
sgalway00 opened this issue May 28, 2020 · 4 comments
Closed

Bad state: Stream has already been listened to #70

sgalway00 opened this issue May 28, 2020 · 4 comments

Comments

@sgalway00
Copy link

sgalway00 commented May 28, 2020

I have successfully implemented your package but when I try to start the isolate a second time I get an error "Bad state: Stream has already been listened to." During the first activation everything works as expected.

static void callback(LocationDto locationDto) async {
    print('location in dart: ${locationDto.toString()}');

    final SendPort send = IsolateNameServer.lookupPortByName(_gpsIsolate);
    send?.send(locationDto);
  }

  void getLocationFromBackground(LocationDto inGpsData) async {
    print(inGpsData);
  }

  void startLocationService() {
    print('startLocationService started');
    BackgroundLocator.registerLocationUpdate(
      callback,
      //optional
      //androidNotificationCallback: notificationCallback,
      settings: LocationSettings(
          //Scroll down to see the different options
          notificationTitle: "Start Location Tracking example",
          notificationMsg: "Track location in background exapmle",
          wakeLockTime: 20,
          autoStop: false,
          interval: 1),
    );
  }

  void startGps2() async {
    print('startGps2 pressed');

    if (IsolateNameServer.lookupPortByName(_gpsIsolate) != null) {
      IsolateNameServer.removePortNameMapping(_gpsIsolate);
    }

     IsolateNameServer.registerPortWithName(
        gpsReceivePort.sendPort, _gpsIsolate);

     startLocationService();

    

     gpsReceivePort.listen((dynamic data) async {
      getLocationFromBackground(data);
      print('Incoming Data: $data');
    });
  }

  void stopGps2() async {
    BackgroundLocator.unRegisterLocationUpdate();
    IsolateNameServer.removePortNameMapping(_gpsIsolate);

    gpsReceivePort.close();

    print('stopGps2 pressed, Isolate stopped');

    
  }

VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: Bad state: Stream has already been listened to. #0 _StreamController._subscribe (dart:async/stream_controller.dart:683:7) #1 _ControllerStream._createSubscription (dart:async/stream_controller.dart:833:19) #2 _StreamImpl.listen (dart:async/stream_impl.dart:475:9) #3 _ReceivePortImpl.listen (dart:isolate-patch/isolate_patch.dart:87:31) #4 GPSBloc.startGps2 package:yonav2/blocs/gpsBloc.dart:161 #5 _GpsSwitchState.build.<anonymous closure>.<anonymous closure> package:yonav2/gpsWidgets/gpsWidgets.dart:62 #6 _rootRunUnary (dart:async/zone.dart:1192:38) #7 _CustomZone.runUnary (dart:async/zone.dart:1085:19) #8 _FutureListener.handleValue (dart:async/future_impl.dart:141:18) #9 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45) #10 Future._propagateToListeners (dart:async/future_impl.dart:711:32) #11 Future._completeWithValue (dart:async/future_impl.da<…>

@leonardoayres
Copy link

Have you tried to close the Port and recreate it?

Move the:
gpsReceivePort.listen((dynamic data) async { getLocationFromBackground(data); print('Incoming Data: $data'); });

to a function and call it to recreate the Port.

@sgalway00
Copy link
Author

From my code snippet above you can see that I do close the port in the stopGps2() function.

I don't understand what you mean my "move to a function". The listener should be in a separate function from where the port is created?

@leonardoayres
Copy link

@sgalway00 you could do something like that:

void _backgroundListener() {
if (gpsReceivePort != null) {
--- your code ---
} else {
--- create the port ---
}
}

But my scenario is that i'm only activate the background_locator when my app go to INACTIVE state, so i have to create and recreate the port in certains conditions.

@mehdok
Copy link
Collaborator

mehdok commented Aug 17, 2020

Closing this issue due inactivity, but feel free to open it anytime;

@mehdok mehdok closed this as completed Aug 17, 2020
jocelyngriselle pushed a commit to sncf-connect-tech/background_locator that referenced this issue Apr 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants