Skip to content

Commit 453c4a0

Browse files
committed
fix: channel
1 parent 458457a commit 453c4a0

File tree

2 files changed

+58
-38
lines changed

2 files changed

+58
-38
lines changed

apps/demo/flutter_module/lib/main.dart

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ class MyApp extends StatelessWidget {
1010
// This widget is the root of your application.
1111
@override
1212
Widget build(BuildContext context) {
13+
14+
NativeScript.getInstance()
15+
.notify("incrementCounter", 0);
16+
1317
return MaterialApp(
1418
title: 'Flutter Demo',
1519
theme: ThemeData(
@@ -75,8 +79,9 @@ class NativeScriptObject {
7579
}
7680
}
7781

82+
const MethodChannel nsc = MethodChannel('org.nativescript.flutter/channel');
83+
7884
class NativeScriptProxy {
79-
static const MethodChannel nsc = MethodChannel('nativescript');
8085
static final Map<Symbol, dynamic> objects = {};
8186

8287
NativeScriptProxy();
@@ -143,22 +148,28 @@ class NativeScriptProxy {
143148
}
144149

145150
class NativeScript {
146-
NativeScript();
151+
NativeScript._();
152+
153+
static NativeScript getInstance(){
154+
return nativeScript;
155+
}
147156

148157
final dynamic native = NativeScriptProxy();
149158

150-
Future<void> notify(String event, dynamic data) async {
151-
return NativeScriptProxy.nsc.invokeMethod<void>("__notify:$event", data);
159+
Future<void> notify(String event, dynamic data) {
160+
return nsc.invokeMethod<void>("__notify:$event", data);
152161
}
153162

154-
Future<void> log(dynamic data) async {
155-
return NativeScriptProxy.nsc.invokeMethod<void>("log", data);
163+
Future<void> log(dynamic data) {
164+
return nsc.invokeMethod<void>("log", data);
156165
}
157166
}
158167

168+
final NativeScript nativeScript = NativeScript._();
169+
159170
class _MyHomePageState extends State<MyHomePage> {
160171
int _counter = 0;
161-
NativeScript nativescript = NativeScript();
172+
NativeScript nativescript = NativeScript.getInstance();
162173

163174
Future<void> _incrementCounter() async {
164175
setState(() {
@@ -178,11 +189,11 @@ class _MyHomePageState extends State<MyHomePage> {
178189
await nativescript
179190
.notify("incrementCounter", _counter);
180191

181-
// int matchParent = await nativescript
182-
// .native.android.widget.LinearLayout.LayoutParams.MATCH_PARENT
183-
// .get();
192+
int matchParent = await nativescript
193+
.native.android.widget.LinearLayout.LayoutParams.MATCH_PARENT
194+
.get();
184195

185-
// await nativescript.log("MATCH_PARENT: $matchParent ${matchParent == -1}");
196+
await nativescript.log("MATCH_PARENT: $matchParent ${matchParent == -1}");
186197

187198
String schemeContent = await nativescript
188199
.native.android.content.ContentResolver.SCHEME_CONTENT

packages/flutter/index.android.ts

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Application, Observable, Utils, fromObject } from '@nativescript/core';
22
import { FlutterCommon } from './common';
3-
43
function makeFragmentName(viewId: number): string {
54
return 'android:flutter:' + viewId;
65
}
@@ -10,45 +9,55 @@ let flutterEngine: io.flutter.embedding.engine.FlutterEngine;
109
let channel: io.flutter.plugin.common.MethodChannel;
1110
let listener;
1211
const instances = new Map();
12+
let MethodCallClazz: java.lang.Class<io.flutter.plugin.common.MethodCall>;
13+
let argumentsMethod;
1314
export function init() {
1415
if (!didInit) {
16+
// todo remove remove after fixing runtime;
17+
MethodCallClazz = java.lang.Class.forName('io.flutter.plugin.common.MethodCall');
18+
argumentsMethod = MethodCallClazz.getDeclaredMethod('arguments', []);
19+
1520
flutterEngine = new io.flutter.embedding.engine.FlutterEngine(Utils.android.getApplicationContext());
1621
flutterEngine.getDartExecutor().executeDartEntrypoint(io.flutter.embedding.engine.dart.DartExecutor.DartEntrypoint.createDefault());
1722
io.flutter.embedding.engine.FlutterEngineCache.getInstance().put('default_nativescript', flutterEngine);
1823
io.flutter.embedding.engine.plugins.util.GeneratedPluginRegister.registerGeneratedPlugins(flutterEngine);
19-
channel = new io.flutter.plugin.common.MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), 'nativescript');
24+
channel = new io.flutter.plugin.common.MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), 'org.nativescript.flutter/channel');
2025
listener = new io.flutter.plugin.common.MethodChannel.MethodCallHandler({
2126
onMethodCall(call: io.flutter.plugin.common.MethodCall, result: io.flutter.plugin.common.MethodChannel.Result) {
22-
const method = call.method;
23-
if (method.startsWith('__notify:')) {
24-
try {
27+
try {
28+
const method = call.method;
29+
if (method.startsWith('__notify:')) {
2530
const eventName = method.replace('__notify:', '');
31+
const args = argumentsMethod.invoke(call, null); //call.arguments();
32+
const data = Utils.dataDeserialize(args);
2633
Flutter.events.notify({
2734
eventName,
2835
object: fromObject({}),
29-
data: Utils.dataDeserialize(call.arguments()),
36+
data,
3037
});
3138
result.success(null);
32-
} catch (error) {
33-
result.error('1000', error.message, '');
34-
}
35-
} else if (method === 'log') {
36-
console.log(Utils.dataDeserialize(call.arguments()));
37-
} else {
38-
const data = Utils.dataDeserialize(call.arguments());
39-
const instance: string | null = data?.instance;
40-
if (instance?.startsWith('__nativeNS:')) {
41-
const ret = eval(instance.replace('__nativeNS:', ''));
42-
result.success(Utils.dataSerialize(ret, true));
43-
return;
44-
} else if (instance?.startsWith('__nativeInstance:')) {
45-
const id = instance.replace('__nativeInstance:', '');
46-
const nativeInstance = instances.get(id);
47-
if (nativeInstance) {
48-
} else {
39+
} else if (method === 'log') {
40+
const args = argumentsMethod.invoke(call, null); //call.arguments();
41+
console.log(Utils.dataDeserialize(args));
42+
} else {
43+
const args = argumentsMethod.invoke(call, null); //call.arguments();
44+
const data = Utils.dataDeserialize(args);
45+
const instance: string | null = data?.instance;
46+
if (instance?.startsWith('__nativeNS:')) {
47+
const ret = eval(instance.replace('__nativeNS:', ''));
48+
result.success(Utils.dataSerialize(ret, true));
49+
return;
50+
} else if (instance?.startsWith('__nativeInstance:')) {
51+
const id = instance.replace('__nativeInstance:', '');
52+
const nativeInstance = instances.get(id);
53+
if (nativeInstance) {
54+
} else {
55+
}
4956
}
57+
console.log('method', method, 'arguments', args);
5058
}
51-
console.log('method', method, 'arguments', call.arguments());
59+
} catch (error) {
60+
result.error('1000', error.message, '');
5261
}
5362
},
5463
});
@@ -60,7 +69,7 @@ export function init() {
6069
}
6170

6271
export class Flutter extends FlutterCommon {
63-
_fragment: io.flutter.embedding.android.FlutterFragment & androidx.fragment.app.Fragment;
72+
_fragment: io.flutter.embedding.android.FlutterFragment;
6473
_androidViewId: number = -1;
6574
_activityCallbacks: android.app.Application.ActivityLifecycleCallbacks;
6675
_channel: io.flutter.plugin.common.MethodChannel;
@@ -119,8 +128,8 @@ export class Flutter extends FlutterCommon {
119128

120129
const tr = fm.beginTransaction();
121130

122-
tr.replace(this._androidViewId, this._fragment, name);
131+
tr.replace(this._androidViewId, this._fragment as any, name);
123132

124-
tr.commitAllowingStateLoss();
133+
tr.commit();
125134
}
126135
}

0 commit comments

Comments
 (0)