네이티브 코드 수정 후엔 다시 빌드해야 합니다. ‘Hot Reload’로는 적용되지 않습니다.

아래와 같이 결과가 출력됩니다.

1
2
D/flutter (28207): Hello from Dart
I/flutter (28207): Hello from Native

main.dart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import 'dart:async';

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

void main() => runApp(MainApp());

class MainApp extends StatelessWidget {
  static const helloNative = const MethodChannel('com.example.flutternative/hello');

  Future<Null> _helloNativeBtnPressed() async {
    try {
      final String result = await helloNative.invokeMethod("hello", "Hello from Dart");
      print("$result");
    } on PlatformException catch (e) {// Native에서 result.error() 호출 시
      print("Failed: '${e.message}'.");
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Container(
          child: Center(
            child: RaisedButton(
              child: Text("Hello"),
              onPressed: _helloNativeBtnPressed,
            ),
          ),
        )
    );
  }
}

MainActivity.java (Android)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.example.flutternative;

import android.os.Bundle;
import android.util.Log;

import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
  private static final String CHANNEL = "com.example.flutternative/hello";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);

    new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
      new MethodChannel.MethodCallHandler() {
        @Override
        public void onMethodCall(MethodCall call, MethodChannel.Result result) {
          if (call.method.equals("hello")) {
            String str = (String)call.arguments;
            Log.d("flutter", str);
            result.success("Hello from Native");
          } else {
            result.notImplemented();
          }
        }
      }
    );
  }
}

AppDelegate.m (iOS)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [GeneratedPluginRegistrant registerWithRegistry:self];
   
    FlutterViewController *controller = (FlutterViewController*)self.window.rootViewController;
   
    FlutterMethodChannel *helloChannel = [FlutterMethodChannel
                                            methodChannelWithName:@"com.example.flutternative/hello"
                                            binaryMessenger:controller];
   
    [helloChannel setMethodCallHandler:^(FlutterMethodCall *call, FlutterResult result) {
        if ([@"hello" isEqualToString:call.method]) {
            NSLog(@"%@", (NSString *)call.arguments);
            result(@"Hello from Native");
        } else {
            result(FlutterMethodNotImplemented);
        }
    }];
   
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

@end