如何防止platformViewRegistry出错[flutter-web]

How to prevent error of platformViewRegistry [flutter-web]

我正在尝试在我的网页 (flutter web) 中添加 pdf 视图。这是部分代码

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

代码运行如我所愿,但我得到这样的错误

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'.

有没有办法防止这种错误发生?

编辑 使用analysis_options.yaml

analyzer:
  errors:
    undefined_prefixed_name: ignore

您可以复制粘贴运行下面的完整代码
您可以使用 // ignore: undefined_prefixed_name
代码片段

// 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');

工作演示

完整模拟代码

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'));
  }
}