DynamicLibrary
- Flutter course
- Expert
- Lesson 22/39
- 4 min
Open a C library with DynamicLibrary.open.
DynamicLibrary
DynamicLibrary.open('libmath.so') loads a C library into memory. You search for symbols on the opened library via lookupFunction. .so is the Linux/Android extension, .dylib is macOS, .dll is Windows. Use DynamicLibrary.process() to open the current process.
final lib = DynamicLibrary.open('libmath.so');
// symbol lookups happen on libSymbol lookup
Once the library is loaded, C symbols are found via lib.lookup<NativeFunction>('symbol'). A symbol is a function or variable name on the C side. lookupFunction converts the symbol into a Dart function.
Practice Get the process library
In main, get a library with DynamicLibrary.process() and print its runtimeType.
Starter code
import 'dart:ffi';
void main() {
// Süreci DynamicLibrary ile aç ve runtimeType yazdır
}Sample solution
import 'dart:ffi';
void main() {
final lib = DynamicLibrary.process();
print(lib.runtimeType);
}Expected output
DynamicLibrary
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does DynamicLibrary.open do?
- Creates a widget
- Opens a database
- Makes an HTTP request
- Loads a C library into memory Correct answer
Explanation: DynamicLibrary.open loads a C library (.so/.dylib/.dll) into memory; symbol lookups are done through this object.
2 DynamicLibrary.open call
final lib = ____.open('libmath.so');
final lib = DynamicLibrary.open('libmath.so');
Explanation: DynamicLibrary.open is the only valid constructor; LibraryLoader, NativeLibrary and CLibrary do not exist in dart:ffi.
3 DynamicLibrary matches
| Term | Meaning |
|---|---|
DynamicLibrary |
Loaded library |
open |
Library opening |
symbol |
C-side name |
.so |
Linux/Android extension |