CodeBlanks Logo Icon CodeBlanks

What is FFI?

Call C libraries with dart:ffi.

What is FFI?

dart:ffi lets you call C libraries from Dart code. It binds functions written in C to Dart, providing interop; calls happen directly without bridge code.

import 'dart:ffi';

// In C: int topla(int a, int b);
typedef NativeTopla = Int32 Function(Int32 a, Int32 b);
typedef DartTopla = int Function(int a, int b);

NativeFunction and typedef

You define the C function signature on the Dart side with two typedefs: one for the NativeFunction on the C side (Int32, Pointer), and one for the Dart side (int). The two types are bridged by lookupFunction.

Practice Define FFI typedefs

Define the typedefs NativeTopla = Int32 Function(Int32 a, Int32 b) and DartTopla = int Function(int a, int b). In main, print 'ffi hazir'.

Starter code

main.dart
import 'dart:ffi';

// NativeTopla typedef'i tanımla (Int32 dönüşlü Function)
// DartTopla typedef'i tanımla (int dönüşlü Function)

void main() {
  // 'ffi hazir' yazdır
}

Sample solution

main.dart
import 'dart:ffi';

typedef NativeTopla = Int32 Function(Int32 a, Int32 b);
typedef DartTopla = int Function(int a, int b);

void main() {
  print('ffi hazir');
}

Expected output

ffi hazir

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does dart:ffi provide?

  • Opening a database
  • Calling C libraries from Dart Correct answer
  • Making HTTP requests
  • Drawing widgets

Explanation: dart:ffi binds C functions to Dart so they can be called directly; HTTP, widgets, or databases are not ffi's job.

2 Import dart:ffi

Question
import 'dart:____';
Answer
import 'dart:ffi';

Explanation: The dart:ffi package is used for C interop; dart:io is for files/sockets, dart:async for Future/Stream, and dart:core for basic types.

3 FFI matches

Term Meaning
dart:ffi C interop library
C System programming language
interop Cross-language bridge
NativeFunction C-side signature

All lessons in Expert