@@ -49,12 +49,30 @@ PlatformViewChannel::~PlatformViewChannel() {
4949}
5050
5151void PlatformViewChannel::Dispose () {
52+ ClearViewInstances ();
53+ ClearViewFactories ();
54+ }
55+
56+ void PlatformViewChannel::RemoveViewInstanceIfNeeded (int view_id) {
57+ auto it = view_instances_.find (view_id);
58+ if (view_id >= 0 && it != view_instances_.end ()) {
59+ auto view_instance = it->second ;
60+ view_instance->Dispose ();
61+ delete view_instance;
62+ view_instances_.erase (it);
63+ }
64+ }
65+
66+ void PlatformViewChannel::ClearViewInstances () {
5267 // Clean-up view_instances_
5368 for (auto const & [view_id, view_instance] : view_instances_) {
69+ view_instance->Dispose ();
5470 delete view_instance;
5571 }
5672 view_instances_.clear ();
73+ }
5774
75+ void PlatformViewChannel::ClearViewFactories () {
5876 // Clean-up view_factories_
5977 for (auto const & [view_type, view_factory] : view_factories_) {
6078 view_factory->Dispose ();
@@ -121,6 +139,7 @@ void PlatformViewChannel::HandleMethodCall(
121139 FT_LOGI (
122140 " PlatformViewChannel create viewType: %s id: %d width: %f height: %f " ,
123141 view_type.c_str (), view_id, width, height);
142+ RemoveViewInstanceIfNeeded (view_id);
124143
125144 flutter::EncodableMap values = std::get<flutter::EncodableMap>(arguments);
126145 flutter::EncodableValue value = values[flutter::EncodableValue (" params" )];
@@ -135,12 +154,6 @@ void PlatformViewChannel::HandleMethodCall(
135154 focused_view->second ->SetFocus (false );
136155 }
137156
138- auto existing_view = view_instances_.find (view_id);
139- if (existing_view != view_instances_.end ()) {
140- existing_view->second ->Dispose ();
141- view_instances_.erase (existing_view);
142- }
143-
144157 auto view_instance =
145158 it->second ->Create (view_id, width, height, byte_message);
146159 if (view_instance) {
@@ -173,6 +186,17 @@ void PlatformViewChannel::HandleMethodCall(
173186 } else {
174187 result->Error (" Can't find view id" );
175188 }
189+ } else if (method == " dispose" ) {
190+ int view_id = -1 ;
191+ if (std::holds_alternative<int >(arguments)) {
192+ view_id = std::get<int >(arguments);
193+ };
194+ if (view_id < 0 || view_instances_.find (view_id) == view_instances_.end ()) {
195+ result->Error (" Can't find view id" );
196+ } else {
197+ RemoveViewInstanceIfNeeded (view_id);
198+ result->Success ();
199+ }
176200 } else {
177201 int view_id = -1 ;
178202 if (!GetValueFromEncodableMap (arguments, " id" , &view_id)) {
@@ -182,10 +206,7 @@ void PlatformViewChannel::HandleMethodCall(
182206
183207 auto it = view_instances_.find (view_id);
184208 if (view_id >= 0 && it != view_instances_.end ()) {
185- if (method == " dispose" ) {
186- it->second ->Dispose ();
187- result->Success ();
188- } else if (method == " resize" ) {
209+ if (method == " resize" ) {
189210 double width = 0.0 , height = 0.0 ;
190211 if (!GetValueFromEncodableMap (arguments, " width" , &width) ||
191212 !GetValueFromEncodableMap (arguments, " height" , &height)) {
0 commit comments