Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strong_mode / onPageShow - PageTransitionEvent is not of type (Event) #27814

Closed
MikeMitterer opened this issue Nov 14, 2016 · 1 comment
Closed
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report

Comments

@MikeMitterer
Copy link

            dom.window.onPageShow.listen( (final dom.PageTransitionEvent event) {
                if(event.persisted) {
                    // when page is loaded from back/forward cache
                    // trigger repaint to let layout scroll in safari
                    element.style.overflowY = 'hidden';
                    dom.window.requestAnimationFrame( (_) {
                        element.style.overflowY = '';
                    });
                }
            });

gives me:

ERROR: Type check failed: (final dom.PageTransitionEvent event) {if (event.persisted) {element.style.overflowY = 'hidden'; dom.window.requestAnimationFrame((_) {element.style.overflowY = '';});}} ((PageTransitionEvent) → void) is not of type (Event) → void ([mdl] lib/src/components/MaterialLayout.dart:188)
@kasperl kasperl added the area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). label Nov 16, 2016
@munificent
Copy link
Member

The error message is ugly but correct here. According to the error, listen() expects a callback that it may invoke and pass any Event to ((Event) → void).

You are giving it a callback that declares that it only accepts dom.PageTransitionEvent ((PageTransitionEvent) → void). That isn't statically sound, since listen() is free to pass it some other subclass of Event that isn't a dom.PageTransitionEvent.

We have discussed adding language support to make enable this kind of unsound code. You can see some more discussion here: #27487. I'll close this as a duplicate of that.

To fix your code, you can do:

            dom.window.onPageShow.listen( (Event event) {
                final dom.PageTransitionEvent transitionEvent = event; // <-- downcast, may fail at runtime
                if(transitionEvent.persisted) {
                    // when page is loaded from back/forward cache
                    // trigger repaint to let layout scroll in safari
                    element.style.overflowY = 'hidden';
                    dom.window.requestAnimationFrame( (_) {
                        element.style.overflowY = '';
                    });
                }
            });

@munificent munificent added the closed-duplicate Closed in favor of an existing report label Dec 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report
Projects
None yet
Development

No branches or pull requests

3 participants