Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 views
in Technique[技术] by (71.8m points)

how to pass data from android native code to flutter module?

I am trying to integrate the flutter module in the native code of android. I created the flutter module and create aar file from the module. I successfully imported the module and able to run it. But I am trying to do is to pass parameters from the android native code to the flutter module like this:

public class MainActivity extends FlutterActivity {
    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        MethodChannel channel = new MethodChannel(
                flutterEngine.getDartExecutor().getBinaryMessenger(), "hello"
        );
        channel.invokeMethod("ok", "done");
    }
}

My flutter module:

void main() => runApp(MyHomePage(title: "Testing",));

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  String nativeMessage = '';
  MethodChannel _methodChannel = new MethodChannel("hello");

  @override
  void initState() {
    super.initState();
    _methodChannel.setMethodCallHandler((call) => _incrementCounter(call));
  }

  Future<dynamic> _incrementCounter(MethodCall call) {
    print("FaaiqKhan flutter");
    print("-----------------------------------------------");
    print(call.method);
    print(call.arguments);
    print("-----------------------------------------------");
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Testing",
      home: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                'You have pushed the button this many times:',
              ),
              Text(
                '$_counter',
                style: Theme.of(context).textTheme.headline4,
              ),
            ],
          ),
        ),// This trailing comma makes auto-formatting nicer for build methods.
      ),
    );
  }
}

when I run my android application it always open the flutter application page but didn't print any thing. Did I miss something or I done something wrong.

question from:https://stackoverflow.com/questions/65884935/how-to-pass-data-from-android-native-code-to-flutter-module

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...