-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
NPE 'void io.flutter.plugin.common.MethodChannel.invokeMethod(java.lang.String, java.lang.Object)' on a null object reference #649
Comments
I have something similar, it seems to happen for me when you close the app (using the back button) while a scan is running.
|
I got a simple workaround that may be useful. All it does is stop the currently running scan and add a bit of time to wait for the scan to actually stop. import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_blue/flutter_blue.dart';
class MainPage extends StatelessWidget {
Future<bool> _onWillPop() async {
// A little workaround for issue https://github.com/pauldemarco/flutter_blue/issues/649
if (Platform.isAndroid) {
if (await FlutterBlue.instance.isScanning.first) {
// Also close any open connections you may have.
await FlutterBlue.instance.stopScan();
// not sure if 200 milliseconds is enough on every device. Slower devices may need more time.
await Future.delayed(Duration(milliseconds: 200));
}
}
return true;
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: _onWillPop,
child: // Your body goes here
);
}
} |
I have something similar,This problem should have occurred during the scan~ Back traces starts.
java.lang.NullPointerException: Attempt to invoke virtual method 'void io.flutter.plugin.common.MethodChannel.invokeMethod(java.lang.String, java.lang.Object)' on a null object reference
at com.pauldemarco.flutter_blue.FlutterBluePlugin$5.run(SourceFile:1)
at android.app.Activity.runOnUiThread(Activity.java:7152)
at com.pauldemarco.flutter_blue.FlutterBluePlugin.invokeMethodUIThread(SourceFile:1)
at com.pauldemarco.flutter_blue.FlutterBluePlugin.access$400(SourceFile:1)
at com.pauldemarco.flutter_blue.FlutterBluePlugin$2.onScanResult(SourceFile:6)
at android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper$1.run(BluetoothLeScanner.java:524)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:7896)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
Back traces ends. |
When I go into the app, exit, and click into the app right away, that's where the error is reported, whereas it wouldn't be the case with your approach.Is it possible that the first time I get in, the MethodChannel initializes and calls the system bluetooth scan, exits, comes in right away, the Bluetooth is still scanning, and this time the MethodChannel isn't initialized, it reports this error? |
So what I think happens is this.
All that my workaround does is it tells the FlutterBlue library to stop it's scanning |
At Later, the This is the commit where the issue was introduced: It might be worth at least check if the For testing purposes, the issue can be reproduced easily if the app is brought to the background (when scanning is in the progress) while the |
I have the same issue but when killing/reopening the app when a device was connected on the first run.
|
I'm starting to have doubts whether choosing this library was the right thigh to do.
I've zero idea even where to look for debugging here.
The text was updated successfully, but these errors were encountered: