rmtmckenzie / flutter_native_device_orientation

Native device orientation plugin for flutter.
MIT License
76 stars 63 forks source link

[Android] When smartphone is flat, orientation(useSensor: true) will fail. #23

Open torbenkeller opened 4 years ago

torbenkeller commented 4 years ago

I used to have my smartphone flat on the table while debugging. When I not move the smartphone and want to call the method, the method just fails. It doesn't return and it doesn't throw an Exception. After moving the phone it works as expected, so I think the error is caused of not having a default orientation on init when the vertical and horizontal orientations are overlaying each other.

torbenkeller commented 4 years ago

I investigated the problem a bit more and the function just works when the angle is min 25-30 degrees

busterbogheart commented 3 years ago

Any updates?

Came here to submit this issue... just experienced it in the current version on Android. It awaits (with no timeout) until a new orientation is detected, it seems.

dvagala commented 3 years ago

Same issue here on Android

A possible workaround for the time being

class CurrentNativeOrientation {
  // there's a bug in the package when obtaining current orientation on android, so this is a workaround
  NativeDeviceOrientation? value;
  StreamSubscription<NativeDeviceOrientation>? _stream;

  startListening() {
    _stream = NativeDeviceOrientationCommunicator()
        .onOrientationChanged(useSensor: true)
        .asBroadcastStream()
        .listen((event) {
      value = event;
    });
  }

  stopListening() async {
    await _stream?.cancel();
  }
}
Tomucha commented 3 years ago

It doesn't fail on me, but

  var sensorOrientation = await c.orientation(useSensor: true)

is never resolved.

rmtmckenzie commented 2 years ago

Getting the current orientation is a separate issue which will be resolved in 1.1.2, but I think this bug should be resolved so I'm closing it. Re-open if it isn't!

lore-co commented 2 years ago

Getting the current orientation is a separate issue which will be resolved in 1.1.2, but I think this bug should be resolved so I'm closing it. Re-open if it isn't!

Still not resolved, as when flat, the package still returns a null value

rmtmckenzie commented 2 years ago

@lore-co is this on android? I've (re)fixed the orientation retrieval for android in 1.1.3. The only caveat is that if the device is perfectly still, it won't necessarily return until it is moved (and I haven't figured out a way to get around that yet, unless I always have a sensor listener going which seems like a bad idea for battery life).