CodeBlanks Logo Icon CodeBlanks

Plugin structure

Learn the Dart and native parts of a Flutter plugin.

Plugin structure

A Flutter plugin consists of two sides: the Dart side talks to the app, the native side (Android/iOS) communicates with the system. The two sides are connected via a MethodChannel.

// lib/my_plugin.dart
class MyPlugin {
  static const _channel = MethodChannel('my_plugin');
  static Future<String> getVersion() async {
    return await _channel.invokeMethod('getVersion');
  }
}

MethodChannel bridge

MethodChannel is the bridge between Dart and native. Dart sends a message to native with invokeMethod, and native replies via MethodCall. The channel name must be the same on both sides.

Practice MyPlugin Dart class

Define a class named MyPlugin. Set static const _channel = MethodChannel('my_plugin'). The static Future<String> getVersion() async method should return _channel.invokeMethod('getVersion'). In main, print MyPlugin.

Starter code

main.dart
import 'package:flutter/services.dart';

// MyPlugin sınıfını tanımla
// static const _channel'a 'my_plugin' adlı MethodChannel ata
// version getiren bir static async metod yaz (invokeMethod ile)

void main() {
  // MyPlugin yazdır
}

Sample solution

main.dart
import 'package:flutter/services.dart';

class MyPlugin {
  static const _channel = MethodChannel('my_plugin');

  static Future<String> getVersion() async {
    return await _channel.invokeMethod('getVersion');
  }
}

void main() {
  print(MyPlugin);
}

Expected output

MyPlugin

Questions and answers

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

1 Which two sides does a Flutter plugin consist of?

  • Frontend and backend
  • iOS and only iOS
  • Dart and native Correct answer
  • Web and mobile

Explanation: The plugin's Dart side talks to the app, the native side (Android/iOS) talks to the system; the two are bridged via MethodChannel.

2 Create a MethodChannel

Question
static const _channel = ____('my_plugin');
Answer
static const _channel = MethodChannel('my_plugin');

Explanation: MethodChannel is used for Dart-native method calls; EventChannel is for streams, BasicMessageChannel for low-level messages, and BinaryMessenger is the transport layer.

3 Plugin structure matches

Term Meaning
plugin Two-sided package
Dart App side
native System side
MethodChannel Bridge

All lessons in Expert