Flutter 升级到 1.12 后,使用原生Android组件有些变化,需要更新Android代码
关于 Flutter 如何嵌入Android组件请自行百度。
这里我是修改百度地图。import com.baidu.mapapi.map.MapView;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.StandardMessageCodec;
public final class MapRegistrant {
public static void registerWith(PluginRegistry registry, MapView mapView) {
final String key = MapRegistrant.class.getCanonicalName();
if (registry.hasPlugin(key)) return;
PluginRegistry.Registrar registrar = registry.registrarFor(key);
registrar.platformViewRegistry().registerViewFactory("BDMap", new MapViewFactory(new StandardMessageCodec(), mapView));
}
}
Flutter 升级到 1.12 后,修改如下import com.baidu.mapapi.map.MapView;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.StandardMessageCodec;
public final class MapRegistrant {
public static void registerWith(FlutterEngine flutterEngine, MapView mapView) {
final String key = MapRegistrant.class.getCanonicalName();
ShimPluginRegistry shimPluginRegistry = new ShimPluginRegistry(flutterEngine);
if (shimPluginRegistry.hasPlugin(key)) return;
PluginRegistry.Registrar registrar = shimPluginRegistry.registrarFor(key);
registrar.platformViewRegistry().registerViewFactory("BDMap", new MapViewFactory(new StandardMessageCodec(), mapView));
}
}
registry 改为 flutterEngine,然后使用 ShimPluginRegistry shimPluginRegistry = new ShimPluginRegistry(flutterEngine);
Flutter 1.12 中 MainActivity 使用了 configureFlutterEngine 方法,不再使用 onCreate 方法
在 configureFlutterEngine 方法中使用 如下方式调用public class MainActivity extends FlutterActivity {
@Override
public void configureFlutterEngine(FlutterEngine flutterEngine){
GeneratedPluginRegistrant.registerWith(flutterEngine);
MapView mapView = new MapView(this);
MapRegistrant.registerWith(flutterEngine, mapView);
}
}
关于 Flutter 1.12 和 Android 通信的其它问题,参考 这篇文章