addressOf<T extends NativeType> static method

  1. @Since('3.3')
Pointer<T> addressOf<T extends NativeType>(
  1. @DartRepresentationOf('T') Object native
)

The native address of the implementation of native.

When calling this function, the argument for native must be an expression denoting a variable or function declaration which is annotated with Native. For a variable declaration, the type T must be the same native type as the type argument to that @Native annotation. For a function declaration, the type T must be NativeFunction<F> where F was the type argument to that @Native annotation.

For example, for a native C library exposing a function:

#include <stdint.h>
int64_t sum(int64_t a, int64_t b) { return a + b; }

The following code binds sum to a Dart function declaration, and extracts the address of the native sum implementation:

import 'dart:ffi';

typedef NativeAdd = Int64 Function(Int64, Int64);

@Native<NativeAdd>()
external int sum(int a, int b);

void main() {
  Pointer<NativeFunction<NativeAdd>> addressSum = Native.addressOf(sum);
}

Similarly, for a native C library exposing a global variable:

const char* myString;

The following code binds myString to a top-level variable in Dart, and extracts the address of the underlying native field:

import 'dart:ffi';

@Native()
external Pointer<Char> myString;

void main() {
  // This pointer points to the memory location where the loader has
  // placed the `myString` global itself. To get the string value, read
  // the myString field directly.
  Pointer<Pointer<Char>> addressMyString = Native.addressOf(myString);
}

Implementation

@Since('3.3')
external static Pointer<T> addressOf<T extends NativeType>(
    @DartRepresentationOf('T') Object native);