Update Windows and Linux plugin templates (#53876)
Updates Windows and Linux templates to use the new PluginRegistrarManager, improving lifetime handling and reducing boilerplate.
diff --git a/packages/flutter_tools/templates/plugin/linux.tmpl/projectName_plugin.cc.tmpl b/packages/flutter_tools/templates/plugin/linux.tmpl/projectName_plugin.cc.tmpl
index aab588b..5a0822a 100644
--- a/packages/flutter_tools/templates/plugin/linux.tmpl/projectName_plugin.cc.tmpl
+++ b/packages/flutter_tools/templates/plugin/linux.tmpl/projectName_plugin.cc.tmpl
@@ -72,13 +72,7 @@
void {{pluginClass}}RegisterWithRegistrar(
FlutterDesktopPluginRegistrarRef registrar) {
- // The plugin registrar wrappers owns the plugins, registered callbacks, etc.,
- // so must remain valid for the life of the application.
- static auto *plugin_registrars =
- new std::map<FlutterDesktopPluginRegistrarRef,
- std::unique_ptr<flutter::PluginRegistrarGlfw>>;
- auto insert_result = plugin_registrars->emplace(
- registrar, std::make_unique<flutter::PluginRegistrarGlfw>(registrar));
-
- {{pluginClass}}::RegisterWithRegistrar(insert_result.first->second.get());
+ {{pluginClass}}::RegisterWithRegistrar(
+ flutter::PluginRegistrarManager::GetInstance()
+ ->GetRegistrar<flutter::PluginRegistrarGlfw>(registrar));
}
diff --git a/packages/flutter_tools/templates/plugin/windows.tmpl/projectName_plugin.cpp.tmpl b/packages/flutter_tools/templates/plugin/windows.tmpl/projectName_plugin.cpp.tmpl
index a26aee4..665c916 100644
--- a/packages/flutter_tools/templates/plugin/windows.tmpl/projectName_plugin.cpp.tmpl
+++ b/packages/flutter_tools/templates/plugin/windows.tmpl/projectName_plugin.cpp.tmpl
@@ -83,13 +83,7 @@
void {{pluginClass}}RegisterWithRegistrar(
FlutterDesktopPluginRegistrarRef registrar) {
- // The plugin registrar wrappers owns the plugins, registered callbacks, etc.,
- // so must remain valid for the life of the application.
- static auto *plugin_registrars =
- new std::map<FlutterDesktopPluginRegistrarRef,
- std::unique_ptr<flutter::PluginRegistrarWindows>>;
- auto insert_result = plugin_registrars->emplace(
- registrar, std::make_unique<flutter::PluginRegistrarWindows>(registrar));
-
- {{pluginClass}}::RegisterWithRegistrar(insert_result.first->second.get());
+ {{pluginClass}}::RegisterWithRegistrar(
+ flutter::PluginRegistrarManager::GetInstance()
+ ->GetRegistrar<flutter::PluginRegistrarWindows>(registrar));
}