#android #flutter #dart
Вопрос:
Друзья, я учусь и пытаюсь создать канал между flutter и Android. У меня есть простой код, в котором я могу вызвать макет Android в flutter после нажатия простой кнопки.
Вот мой код:
главная.дротик
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:
Необработанное исключение: Исключение MissingPluginException(Не найдена реализация метода showNativeView на канале com.пример.флаттер/тест)
Что я делаю не так? Я ценю любые комментарии!