Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent error of platformViewRegistry [flutter-web]

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

like image 923
uyhaW Avatar asked Nov 09 '20 08:11

uyhaW


1 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'));
  }
}
like image 56
chunhunghan Avatar answered Nov 09 '22 15:11

chunhunghan