0

I'm trying to take value from the method channel and using the value I'm trying to navigate another screen. When I try to navigate from TextButton onclick it's navigating but when I try to navigate from the value received by the method channel it's not navigating to another screen.

Example: I'm receiving openScreen1 from the method channel in the below code from methodCall.method and assigning the method to route variable but the page is not navigating

main_screen.dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:gg_app/screen1.dart';
import 'package:gg_app/screen2.dart';

class HomeScreen extends StatefulWidget {
  static const routeName = "Home-Screen";
  const HomeScreen({Key? key}) : super(key: key);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  static const channel = MethodChannel('scribeemr.in/mic');

  @override
  void initState() {
    // TODO: implement initState
    channel.setMethodCallHandler(nativeMethodCallHandler);
    super.initState();
  }

  Future<dynamic> nativeMethodCallHandler(MethodCall methodCall) async {
    var route = methodCall.method;
    await navigateTo(route, context);
  }

  Future<dynamic> navigateTo(String route, BuildContext context) async {
    switch (route) {
      case "openScreen1":
        await Navigator.of(context).pushNamed(Screen1.routeName);
        break;
      case "openScreen2":
        await Navigator.of(context).pushNamed(Screen2.routeName);
        break;
      default:
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Home Screen")),
      body: Column(
        children: [
          TextButton(
              onPressed: () {
                navigateTo("openScreen1", context);
              },
              child: Text("Screen 1")),
          TextButton(
              onPressed: () {
                navigateTo("openScreen2", context);
              },
              child: Text("Screen 2")),
        ],
      ),
    );
  }
}

main.dart

import 'package:flutter/material.dart';
import 'package:gg_app/home_screen.dart';
import 'package:gg_app/screen1.dart';
import 'package:gg_app/screen2.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomeScreen(),
      routes: {
        HomeScreen.routeName: (context) => HomeScreen(),
        Screen1.routeName: (context) => Screen1(),
        Screen2.routeName: (context) => Screen2(),
      },
    );
  }
}

screen1.dart

import 'package:flutter/material.dart';

class Screen1 extends StatefulWidget {
  static const routeName = "Screen1";
  const Screen1({ Key? key }) : super(key: key);

  @override
  State<Screen1> createState() => _Screen1State();
}

class _Screen1State extends State<Screen1> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Screen 1")),
    );
  }
}
thinkcode
  • 196
  • 2
  • 9
  • Are you sure the result from method call matches with switch case statements? – Usama Karim Jul 14 '22 at 08:21
  • Where you call the methods? – amir_a14 Jul 14 '22 at 08:30
  • It is because of context try using future. delayed with the duration zero on your init state or save context locally before navigating. You can also try using a package like Get if you only want to solve your problem non optimally – sid Jul 14 '22 at 08:35
  • @sid fluture.delayed didn't work. how to save context locally? – thinkcode Jul 14 '22 at 08:52
  • @thinkcode alternatively you can ask context argument in `nativeMethodCallHandler` and `SchedulerBinding.instance.addPostFrameCallback((_) { channel.setMethodCallHandler((c)=> nativeMethodCallHandler(c,context)); });` on initstate – sid Jul 14 '22 at 09:39

0 Answers0