I have read this https://github.com/flutter/flutter/wiki/Add-Flutter-to-existing-apps.
My problem is to pass data from existing native android app to flutter module (such as: token, username...etc). So, I want to ask if there is any way to pass data between Native code in existing native app and code in flutter module?
For example, there are two pages, A and B, A is written in Java code, B embed flutter view, I didn't find any way to pass data from A to the flutter view in B.
public class TwoActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.two_activity);
//this params passed from HomeActivity
String params = getIntent().getStringExtra("params");
FrameLayout rootView = findViewById(R.id.container);
View flutterView = Flutter.createView(this, getLifecycle(), "service");
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
rootView.addView(flutterView, layoutParams);
}
}
this is main.dart
void main() => runApp(chooseWidget(window.defaultRouteName));
Widget chooseWidget(String route) {
switch(route) {
case 'service':
return MyFlutterView();
}
}
class MyFlutterView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
static const platform = const MethodChannel('samples.flutter.dev/start');
@override
Widget build(BuildContext context) {
return Container(
color: Colors.white,
child: Column(
children: <Widget>[
Text(
'this is a flutter page',
style: TextStyle(
fontSize: 14,
color: Colors.blue
),
),
FlatButton(
onPressed: () {
platform.invokeMethod('startActivity');
},
child: Text('go native page'),
color: Colors.purple,
highlightColor: Colors.deepPurple,
)
],
),
);
}
}
-------------------------->edit in 2019.7.18<-----------------------------
Thank you for your help. I found the answer.
1、BasicMessageChannel:use this to pass string or other object.
2、MethodChannel:use this to method invocation
3、EventChannel: use this to event streams
You can use method channel for this
private static final String CHANNEL = "AndySample/test";
FlutterEngine flutterEngine=new FlutterEngine(this);
flutterEngine.getDartExecutor().executeDartEntrypoint(
DartExecutor.DartEntrypoint.createDefault()
);
FlutterEngineCache
.getInstance()
.put("my_engine_id", flutterEngine);
flutterEngine.getNavigationChannel().setInitialRoute("/");
MethodChannel mc=new
MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(),CHANNEL);
mc.setMethodCallHandler((methodCall, result) ->
{
if(methodCall.method.equals("test"))
{
result.success("Hai from android and this is the data yopu sent me "+ methodCall.argument("data"));
//Accessing data sent from flutter
}
else
{
Log.i("new method came",methodCall.method);
}
}
);
use this blog to know more
https://medium.com/@andymobitec/flutter-sending-data-between-android-and-flutter-1e0693fbae64
You can pass data with method channel, Yes I am late for answer but it might be useful for other devs.
In native you need to invoke method channel, below is example of android.
val testChannel = MethodChannel(flutterEngine!!.dartExecutor.binaryMessenger, "method_channel_name")
testChannel.invokeMethod("method_name", referrerUrl);
In Flutter you can listen this invoked method.
static const platform = const MethodChannel('method_channel_name');
platform.setMethodCallHandler((call)async{
// you can get hear method and passed arguments with method
print("init state setMethodCallHandler ${call.arguments}");
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With