Flutter 如何防止platformViewRegistry[Flatter web]出错

Flutter 如何防止platformViewRegistry[Flatter web]出错,flutter,pdf,flutter-web,Flutter,Pdf,Flutter Web,我正在尝试在我的网页(颤振网页)中添加pdf视图。。这是代码的一部分 ui.platformViewRegistry.registerViewFactory( 'hello-world-html', (int viewId) => html.IFrameElement() ..width = '700' ..height = '4000' ..src = 'http:xxx.pdf'

我正在尝试在我的网页(颤振网页)中添加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'.
有没有办法防止这种错误发生


编辑使用
分析选项。yaml

analyzer:
  errors:
    undefined_prefixed_name: ignore

您可以复制粘贴运行下面的完整代码
您可以使用
//忽略:未定义的\u前缀\u名称

代码片段

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

谢谢你的回答,我已经试过你的代码了。。它运行得很好。。。但我的编辑器中仍然有红色下划线,它仍然说这是因为前缀或……,我编辑了我的问题并添加了一张红色下划线的照片。。虽然代码运行正常,但这是否可以防止出现红色下划线?@uyhaW请尝试重新启动分析器或ide。@uyhaW,请使用analysis\u options.yaml,查看我的更新版本answer@AbhilashChandran,我已经阅读了你给我的链接,@chunhunghan我也遵循了你的答案,最后我通过添加FakeUi.dart找到了解决方案,RealUi.dart,最后一个是在与
pubspec.yaml
相同的文件夹中创建
analysis\u options.yaml
。。。而且效果非常好。。。红色下划线已经消失了。。。谢谢你,真管用!非常感谢。作为让分析仪满意的附加选项,您可以尝试本文中定义的解决方案。检查
dart\u ui.dart
dart\u ui\u fake.dart
dart\u ui\u real.dart
。它是条件导入和垫片的组合。这个问题已经为人所知,并在Github中被跟踪。