How to implement a channel between flutter and android in a StatefulWidget class?

Friends, I’m studying and trying to create a channel between flutter and android. I have a simple code where I can call an android layout in flutter after clicking a simple button.

Here is my code:

main.dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter-Native Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Channel demo app'),
    );
  }
}

class MyHomePage extends StatelessWidget {
  static const platform = const MethodChannel(
      'com.example.flutter/test');
  final String title;

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

  @override
  Widget build(BuildContext 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');
    }

  Future<dynamic> _handleMethod(MethodCall call) async {
    switch(call.method) {
      case "message":
        debugPrint(call.arguments);
        return new Future.value("");
    }
  }
}

ActivityMain.java

package com.example.teste_flutter_android_camera02;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {

    public static final String CHANNEL = "com.example.flutter/test";

    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        GeneratedPluginRegistrant.registerWith(flutterEngine);
        new MethodChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(),
                CHANNEL).setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
                if(call.method.equals("showNativeView")){
                    Intent i = new Intent(MainActivity.this, DemoCamActivity.class);
                    startActivity(i);
                    result.success(true);
                }else{
                    result.notImplemented();
                }
            }
        });
    }
}

This works perfectly.

But now I’m trying to create a channel using StatefulWidget, but it doesn’t work!
I did it like this:

main.dart

class HomePage extends StatefulWidget {
  final List<Class> class;

  const HomePage({Key key, this.class}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState(aula);
}

class _HomePageState extends State<HomePage> {

  static const platform = const MethodChannel(
      'com.example.flutter/test');

  var isLoading = false;
  final List<Aula> aula;

  _HomePageState(this.class){
    platform.setMethodCallHandler(handleMethod);
  }

  Future<Null> showNativeView() async {
    await platform.invokeMethod('showNativeView');
  }

  Future<dynamic> handleMethod(MethodCall call) async {
    switch(call.method) {
      case "message":
        debugPrint(call.arguments);
        return new Future.value("");
    }
  }

.
.
.
.
          new RaisedButton(
            child: new Text('Show native view'),
            onPressed: showNativeView,
          ),

I use the same MainActivity class. Java

However I get the error:

Unhandled Exception: MissingPluginException(No implementation found
for method showNativeView on channel
com.example.flutter/test)

What am I doing wrong? I appreciate any comments!

I’m also facing the same problem. Are you using the same channel in two classes? I don’t face any issue when running it from a stateful widget.