How to prevent error of platformViewRegistry [flutter-web]

Viewed 7304

I am trying to add pdf view in my web page (flutter web).. here is part of the code

ui.platformViewRegistry.registerViewFactory(
        'hello-world-html',
        (int viewId) => html.IFrameElement()
          ..width = '700'
          ..height = '4000'
          ..src = 'http:xxx.pdf'
          ..style.border = 'none');

the code runs like what I want, but I get error like this

The name 'platformViewRegistry' is being referenced through the prefix 'ui', but it isn't defined in any of the libraries imported using that prefix.
Try correcting the prefix or importing the library that defines 'platformViewRegistry'.

is there a way to prevent that error happen?

enter image description here

2 Answers

Edit use analysis_options.yaml

analyzer:
  errors:
    undefined_prefixed_name: ignore

enter image description here

You can copy paste run full code below
You can use // ignore: undefined_prefixed_name
code snippet

// ignore: undefined_prefixed_name
ui.platformViewRegistry.registerViewFactory(
        'hello-world-html',
        (int viewId) => html.IFrameElement()
          ..width = '700'
          ..height = '4000'
          ..src = 'http:xxx.pdf'
          ..style.border = 'none');

working demo

enter image description here

full simulate code

import 'package:flutter/material.dart';
// import 'dart:io' if (dart.library.html) 'dart:ui' as ui;
import 'dart:ui' as ui;
import 'dart:html' as html;

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(child: Iframe()),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

class Iframe extends StatelessWidget {
  Iframe() {
    // ignore: undefined_prefixed_name
    ui.platformViewRegistry.registerViewFactory('iframe', (int viewId) {
      var iframe = html.IFrameElement();
      iframe.src = 'http://www.africau.edu/images/default/sample.pdf';
      return iframe;
    });
  }
  @override
  Widget build(BuildContext context) {
    return Container(
        width: 400, height: 300, child: HtmlElementView(viewType: 'iframe'));
  }
}

chunhunghan's proposed fix of ignoring undefined_prefix_name is overkill and risky. If you do this, all errors of the type prefix.wrongValue will be suppressed in your IDE. If you're on a small/personal project, this is fine, but if you're working on a larger scale project I would advise against this.

A better solution is to create a shim file for not-web contexts like so:

platform_view_registry.dart

export 'package:my_package/src/fake_platform_view_registry.dart'
  if (dart.library.html) 'dart:ui' show platformViewRegistry;

fake_platform_view_registry.dart

// add either a dynamically typed variable
dynamic platformViewRegistry;

// or a more thorough shim like this one
class FakePlatformViewRegistry {
  void registerViewFactory(
      String viewTypeId, dynamic Function(int) viewFactory) {
         throw UnsupportedError("platform view registry in non-web context");
      }
}
final platformViewRegistry = FakePlatformViewRegistry();

Then just import package:my_package/src/platform_view_registry.dart instead of dart:ui (and probably drop the ui prefix too).

import "package:my_package/src/platform_view_registry.dart";

...

platformViewRegistry.registerViewFactory(
        'hello-world-html',
        (int viewId) => html.IFrameElement()
          ..width = '700'
          ..height = '4000'
          ..src = 'http:xxx.pdf'
          ..style.border = 'none');
Related