huxiaofan1223
huxiaofan1223

Reputation: 1

The provider instance seems to have been taken incorrectly in methodChannel.setMethodCallHandler

class MainActivity : FlutterActivity() {
    private final val CHANNEL = "com.example/mediaControl";
    private lateinit var flutterEngine: FlutterEngine;
    lateinit var channel:MethodChannel;
    private fun sendKeyCodeToFlutter(keyCode: String) {
        channel.invokeMethod(
            "sendKeyCode",
            keyCode
        )
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        flutterEngine = FlutterEngine(this);
        flutterEngine.getDartExecutor().executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault());
        channel = MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL);
        Handler().postDelayed({
            sendKeyCodeToFlutter("countAdd");
        }, 3000)
        super.onCreate(savedInstanceState)
    }
}
void main() {
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => Counter()),
      ],
      child: const MyApp(),
    ),
  );
}
class Counter with ChangeNotifier, DiagnosticableTreeMixin {
  int _count = 0;
  int get count => _count;
  void increment() {
    _count++;
    notifyListeners();
  }
}
class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}
class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
    var _context = context;
    const MethodChannel _channel = MethodChannel('com.example/mediaControl');
    _channel.setMethodCallHandler((call) async {
      switch (call.method) {
        case 'sendKeyCode':
          if (call.arguments == 'countAdd') {
            Provider.of<Counter>(context, listen: false).increment();
            print('receive: countAdd');
            print('receive: ${Provider.of<Counter>(context, listen: false).count}');
          }
          break;
        default:
          throw UnimplementedError();
      }
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('${context.watch<Counter>().count}')
          ],
        ),
      )
    );
  }
}

I want to send a message to the Flutter side through Android to trigger a certain function of the provider instance in the Flutter side. The result is that it can be triggered, but it seems that the instance is not obtained correctly, and the count on the page has not been updated

I want to send a message to the Flutter side through Android to trigger a certain function of the provider instance in the Flutter side. The result is that it can be triggered, but it seems that the instance is not obtained correctly, and the count on the page has not been updated

Upvotes: 0

Views: 23

Answers (0)

Related Questions