Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Update "unregistered_view_type" error message #33450

Merged
merged 1 commit into from
May 23, 2022

Conversation

dacianf
Copy link
Contributor

@dacianf dacianf commented May 18, 2022

Fix for #100241

Updating the error message for unregistered_view_type to include a hint about a very common cause.

Closes #100241

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide and the C++, Objective-C, Java style guides.
  • I listed at least one issue that this PR fixes in the description above.
  • I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test-exempt. See testing the engine for instructions on
    writing and running engine tests.
  • I updated/added relevant documentation (doc comments with ///).
  • I signed the CLA.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@flutter-dashboard flutter-dashboard bot added platform-ios platform-macos platform-web Code specifically for the web engine labels May 18, 2022
@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@Hixie
Copy link
Contributor

Hixie commented May 18, 2022

test-exempt: improves error message

This is an area where you could test it, e.g. by triggering the error and seeing if the message has the detail you want, but it's fine if you don't want to do that.

@@ -140,7 +140,8 @@
NSObject<FlutterPlatformViewFactory>* factory = factories_[viewType].get();
if (factory == nil) {
result([FlutterError errorWithCode:@"unregistered_view_type"
message:@"trying to create a view with an unregistered type"
message:@"trying to create a view with an unregistered type. Have "
@"you called GeneratedPluginRegistrant.register?"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cyanglaz in the wild we saw this error fixed by calling GeneratedPluginRegistrant.register flutter/flutter#100241 (comment) but perhaps there's also some -registerViewFactory: to be done in the darwin case?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I know it exists, the question is when this error happens is it reasonable to say "you need to call GeneratedPluginRegistrant.register" because it's missing (add-to-app) or do they also need to call -registerViewFactory: somewhere? We want this suggestion to actually be actionable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok.

The registerViewFactory: is only meant to be called by the author of the PlatformView. If the user only uses a PlatformView plugin, they wouldn't be able to call registerViewFactory: as they don't own the factory, so showing this to the app developer who uses the PlatformView plugin might cause confusion.
I think only include GeneratedPluginRegistrant.register is fine for app developers.

We might be able to add some checks for the plugin/PlatformView authors to remind them to call registerViewFactory in registerWithRegistrar.

In addition to that, I think this part is also confusing: "trying to create a view with an unregistered type".
What view?(A view means lots of things, especially in add-to-app) Who or what tried to create a view? Maybe something like:

A UIKitView widget is trying to create a PlatformView with an unregistered type: <some type>. 
If you are the author of the PlatformView, make sure `registerViewFactory` is invoked. See: https://docs.flutter.dev/development/platform-integration/platform-views?tab=ios-platform-views-objective-c-tab#on-the-platform-side-1 for more details.
If you are not the author of the PlatformView, make sure to call `GeneratedPluginRegistrant.register`. 

Copy link
Member

@jmagman jmagman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some nits about whitespace and the URL

@@ -140,9 +140,9 @@
NSObject<FlutterPlatformViewFactory>* factory = factories_[viewType].get();
if (factory == nil) {
result([FlutterError errorWithCode:@"unregistered_view_type"
message:[NSString stringWithFormat:@"A UIKitView widget is trying to create a PlatformView with an unregistered type: < %@ >", viewType]
message:[NSString stringWithFormat:@"A UIKitView widget is trying to create a PlatformView with an unregistered type: < %@ >", args[@"viewType"]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you pull args[@"viewType"] into a local above std::string viewType([args[@"viewType"] UTF8String]); and use it here to reduce the complexity of this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmagman std::string viewType([args[@"viewType"] UTF8String]); is already defined on line 132. The problem that I noticed by using only viewType as variable was that the build failed with "error: cannot pass non-trivial object of type 'std::string'". So I guess it needs an NSString and not an std::String

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I think it should work if I use %s instead of %@

Copy link
Member

@jmagman jmagman May 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use a %s c string like that, there are encoding implications https://stackoverflow.com/a/60217425

I was suggesting

NSString* viewTypeString = args[@"viewType"];
std::string viewType(viewTypeString.UTF8String);
...
              message:[NSString stringWithFormat:@"A UIKitView widget is trying to create a "
                                                 @"PlatformView with an unregistered type: < %@ >",
                                                 viewTypeString]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmagman I added the NSString variable there. I was thinking that maybe I have to make it work using the existing variable.

Copy link
Member

@ditman ditman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM once @jmagman and @cyanglaz are happy!

Copy link
Member

@jmagman jmagman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more nit, then LGTM

@dacianf dacianf requested a review from jmagman May 21, 2022 06:22
message:[NSString stringWithFormat:@"A UIKitView widget is trying to create a "
@"PlatformView with an unregistered type: < %@ >",
viewTypeString]
details:@"If you are the author of the PlatformView, make sure `registerViewFactory`"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you double check the spacing on these?

Suggested change
details:@"If you are the author of the PlatformView, make sure `registerViewFactory`"
details:@"If you are the author of the PlatformView, make sure `registerViewFactory` "

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

message:[NSString stringWithFormat:@"A UIKitView widget is trying to create a "
@"PlatformView with an unregistered type: < %@ >",
viewType]
details:@"If you are the author of the PlatformView, make sure `registerViewFactory`"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
details:@"If you are the author of the PlatformView, make sure `registerViewFactory`"
details:@"If you are the author of the PlatformView, make sure `registerViewFactory` "

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@jmagman jmagman added the waiting for tree to go green This PR is approved and tested, but waiting for the tree to be green to land. label May 23, 2022
@fluttergithubbot fluttergithubbot merged commit c12332b into flutter:main May 23, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 23, 2022
houhuayong pushed a commit to houhuayong/engine that referenced this pull request Jun 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
platform-ios platform-macos platform-web Code specifically for the web engine waiting for tree to go green This PR is approved and tested, but waiting for the tree to be green to land.
Projects
None yet
6 participants