0

i am done with communication between flutter to native code using MethodChannel. Its bridge between flutter to native done but when i try to redirection of Native Screen to Flutter screen its not redirect. I am using Navigator push method to redirect screen. Please check below code :

class MyHomePage extends StatelessWidget {

  BuildContext mcontext;

  static const platform = const MethodChannel(
      'flutter.rortega.com.basicchannelcommunication');
  final String title;

  MyHomePage({Key key, this.title}) : super(key: key) {
    platform.setMethodCallHandler(_handleMethod);
  }


  @override
  Widget build(BuildContext context) {
    mcontext = context;
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(title),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new RaisedButton(
              child: new Text('Show native view'),
              onPressed: _showNativeView,
            ),
          ],
        ),
      ),
    );
  }

  Future<Null> _showNativeView() async {
    await platform.invokeMethod('showNativeView', {"text": "Maulik"});
  }

  Future<dynamic> _handleMethod(MethodCall call) async {
    switch (call.method) {
      case "message":
        String alice = call.arguments['message'];

        print(alice);
        pushPreviewScreen(mcontext);
    }
  }

  pushPreviewScreen(BuildContext mcontext) {
    print("calledFunction::");
    Navigator.push(
      mcontext,
      MaterialPageRoute(builder: (context) => SecondScreen()),
    );
  }
}

Here "calledFunction::" print in console but not redirect in SecondScreen().

Maulik p
  • 26
  • 1
  • 2

1 Answers1

0

Try using it this way, Using then Statement

   await platform.invokeMethod('showNativeView', {"text": "Maulik"}).then((onValue) {
      //you can also check the returned `value` from native `code` and return `true` or `false` from the `native` `code`, and on the basis of that you can send the user to the screen like `if(value)` //send to home screen else //send back to screen
      Navigator.push(
        mcontext,
        MaterialPageRoute(builder: (context) => SecondScreen()),
      );
    });

And If that doesn't work too and you are getting any error related to setState called after dispose then try this,

       await platform.invokeMethod('showNativeView', {"text": "Maulik"}).then((onValue) {
          if (this.mounted) {
             setState(() {
                // Your Navigation Code Here
             });
           }
        });
Mukul
  • 1,020
  • 6
  • 12