stacktrace property Null safety

String? stacktrace
final

Native stacktrace for the error, possibly null.

This contains the native platform stack trace, not the Dart stack trace.

The stack trace for Dart exceptions can be obtained using try-catch blocks, for example:

try {
  // ...
} catch (e, stacktrace) {
  print(stacktrace);
}

On Android this field is populated when a RuntimeException or a subclass of it is thrown in the method call handler, as shown in the following example:

import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

class MainActivity: FlutterActivity() {
  private val CHANNEL = "channel_name"

  override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
    super.configureFlutterEngine(flutterEngine)
    MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
      call, result -> throw RuntimeException("Oh no")
    }
  }
}

It is also populated on Android if the method channel result is not serializable. If the result is not serializable, an exception gets thrown during the serialization process. This can be seen in the following example:

import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

class MainActivity: FlutterActivity() {
  private val CHANNEL = "channel_name"

  override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
    super.configureFlutterEngine(flutterEngine)
    MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
      call, result -> result.success(Object())
    }
  }
}

In the cases described above, the content of stacktrace will be the unprocessed output of calling toString() on the exception.

MacOS, iOS, Linux and Windows don't support querying the native stacktrace.

On custom Flutter embedders this value will be null on platforms that don't support querying the call stack.

Implementation

final String? stacktrace;