Avoid using web-only libraries outside Flutter web plugin packages - html

I'm building a Flutter app that I am trying to make work on the web. Part of it contains some web specific code:
import 'dart:html' as html;
import 'package:flutter/foundation.dart';
class DownloadViewModel extends ChangeNotifier {
static const String url = 'https://example.com/api/v1/app/myapp_1.0.0.apk';
void onAndroidDownloadPressed() {
html.window.open(url, 'AndroidApp');
}
}
However the dart:html import gives the following error:
Avoid using web-only libraries outside Flutter web plugin packages
The longer version of the warning looks like this:
Avoid using web libraries, dart:html, dart:js and dart:js_util in
Flutter packages that are not web plugins. These libraries are not
supported outside a web context; functionality that depends on them
will fail at runtime in Flutter mobile, and their use is generally
discouraged in Flutter web.
Web library access is allowed in:
plugin packages that declare web as a supported context
otherwise, imports of dart:html, dart:js and dart:js_util are disallowed.
And it's not just a warning. This actually prevents building an Android or iOS app (even though this method isn't accessible from non-Web Flutter apps).
The only solution I've figured out is to comment out the import when I am building for Android and iOS and then uncomment it when I am building for the web. Is there a better solution?

Use the universal_html package. It supports the browser, Dart VM, and Flutter and is a stand-in replacement for dart:html and other web related libraries.
dependencies:
universal_html: ^1.2.1
Then instead of using import 'dart:html' as html; you can use the following import:
import 'package:universal_html/html.dart' as html;
For those who came to this page for other related web import problems (like dart:js), this plugin also supports the following imports:
import 'package:universal_html/driver.dart';
import 'package:universal_html/html.dart';
import 'package:universal_html/indexed_db.dart';
import 'package:universal_html/js.dart';
import 'package:universal_html/js_util.dart';
import 'package:universal_html/prefer_sdk/html.dart';
import 'package:universal_html/prefer_sdk/indexed_db.dart';
import 'package:universal_html/prefer_sdk/js.dart';
import 'package:universal_html/prefer_sdk/js_util.dart';
import 'package:universal_html/prefer_sdk/svg.dart';
import 'package:universal_html/prefer_sdk/web_gl.dart';
import 'package:universal_html/prefer_universal/html.dart';
import 'package:universal_html/prefer_universal/indexed_db.dart';
import 'package:universal_html/prefer_universal/js.dart';
import 'package:universal_html/prefer_universal/js_util.dart';
import 'package:universal_html/prefer_universal/svg.dart';
import 'package:universal_html/prefer_universal/web_gl.dart';
import 'package:universal_html/svg.dart';
import 'package:universal_html/web_gl.dart';

Since the merge of Flutter-web into the main Flutter repository, it's no longer possible to directly add imports for web libraries (e.g. dart:html, or dart:js) in a Flutter project on the main channel when targeting Web, Android and iOS.
Use the universal html package which provides extensive support for multiple platforms and web libraries.
From the root level of your project, command
flutter pub add universal_html
import 'package:universal_html/html.dart' as html
This package isn't required to run some web files (e.g. dart:js). In my case, I just had to remove the import 'dart:js' import statement.

Related

How to import a page within a page in Ionic 3

This is the app component. It gives an error that it can't find module pages/question/pages/question1/question1
This is the app module where I declared the question1. still the same error.
Your import path is wrong, it should be as,
import {question1} from './pages/questions/pages/question1'

How can I establish a plugin ecosystem using ES6 modules?

I want to publish a JS library and let anyone develop plugins for the library.
For example, here's a library I publish:
// awesome-lib.js
export class Library {
registerPlugin(plugin) {
...
}
}
export class PluginBase {
...
}
And a plugin implemented by other developer:
// third-party-plugin.js
import {PluginBase} from './awesome-lib';
export default MyPlugin extends PluginBase {
...
}
And the end-user's code would be something like this:
import {Library} from './awesome-lib';
import MyPlugin from './third-party-plugin';
const lib = new Library();
lib.registerPlugin(new MyPlugin());
However, there's a restriction that the end-user needs to deploy the awesome-lib.js and third-party-plugin in the same directory because the latter imports PluginBase from the awesome-lib.js in the same directory.
In Node.js, this kind of plugin is easy because its module discovery is managed by npm. But in browsers, things get difficult as its module discovery only relies on relative/absolute paths.
Is there any solution for this?

Scala NoClassDefFoundError related to Swing

I have a GUI class named Gui.scala and Eclipse does not show any errors directly in the scala file. My Scala version is 2.11 and I have manually added scala-swing-2.10.4.jar to my build path in Eclipse. If I don't do this, Eclipse complains that it does not find the Swing library.
First lines of my source code look like this:
package filmreviews
import scala.swing._
import event._
import javax.imageio.ImageIO
import javax.swing.ImageIcon
import java.io.File
import java.awt.image.BufferedImage
import java.net.URI
import java.net.URL
import java.awt.Desktop
import java.awt.Cursor
import java.awt.Color
import javax.swing.border
import javax.swing.BorderFactory
import javax.swing.UIManager
object Gui extends SimpleSwingApplication {
def top = new MainFrame {
...more code here...
I get the following error:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: scala/collection/GenTraversableOnce$class
Also, there is a long list telling where in code the error happened. Only lines referring to my own code are:
at filmreviews.Gui$$anon$3.<init>(Gui.scala:17)
at filmreviews.Gui$.top(Gui.scala:17)
at filmreviews.Gui$.top(Gui.scala:16)
This is why I think it is related to the creation of the MainFrame object. It can also be related to how I have manually added the Swing library to the build path. However, I don't know what causes the error or how to fix it.
If you're using Scala 2.11, you will need a version of scala-swing built for 2.11. 2.10.4 is binary incompatible with 2.11.
You can find the jar for a 2.11-compatible version on maven central.
Or for those using sbt:
libraryDependencies += "org.scala-lang.modules" %% "scala-swing" % "1.0.1"

Geronimo FTP Server on Fedora 19

I am trying to use the FTP server (factory) in Geronimo 3.0.1 on Fedora 19, in eclipse kepler. I have the following import which produces no error:
import org.apache.mina.*;
However, when I declare
FTPServerFactory ftpFactory;
FTPServer ftpServer;
neither of FTPServer and FTPServerFactory is resolvable. The usual eclipse hints in the editor, which are very cool, offer no help in this case. My build path has the mina-core.jar (This is the only MINA jar that I find in /usr/share/java/apache-mina). The build path dialog flags errors, not explicitly for mina, stating the the following are missing:
org.eclipse.JRE_CONTAINER/
org.eclipse.jdt.internal.debug.uio.launcher.StandardVMType/
java-1.7.0-openjdk-1.7.0.25.x86-64
I suspect that my installation is missing other mina jars and am at a loss for the three errors above except that the last one is strange given that the that the build path has
java-1.7.0-openjdk-1.7.0
My environment is all relatively new, so there could be problems in a number of places. Any advice on where to start?
Thanks in advance.
I am not sure what happened when I logged in. Please disregard the empty question.
I have the following, which does not produce errors.
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.FtpReply;
import org.apache.ftpserver.ftplet.FtpRequest;
import org.apache.ftpserver.ftplet.FtpSession;
import org.apache.ftpserver.ftplet.Ftplet;
import org.apache.ftpserver.ftplet.FtpletContext;
import org.apache.ftpserver.ftplet.FtpletResult;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.ssl.SslConfigurationFactory;
import org.apache.ftpserver.usermanager.*;
import org.apache.ftpserver.usermanager.impl.BaseUser;
My build path includes
ftpserver-core-1.06.jar - /usr/share/java/apache-ftpserver/common/lib
A code fragment follows
//Add the user to the FTP server as well.
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
userManagerFactory.setFile(new File("myusers.properties"));
userManagerFactory.setPasswordEncryptor(new SaltedPasswordEncryptor());
org.apache.ftpserver.ftplet.UserManager um = userManagerFactory.createUserManager();
BaseUser user = new BaseUser();
user.setName(newCredentials.getUserID());
user.setPassword(ConfigurationValues.get("ftpGenericPassword"));
new File(ConfigurationValues.get("ftpFilesRoot")+newCredentials.getUserID());
user.setHomeDirectory("ftproot");
um.save(user);
I hope this is of use. Takes a little burrowing to sort it out.

Can't import sikuli modules from Sikuli IDE 1.0.0

I'm using Sikuli IDE 1.0.0 on Mac, trying to get a simple test case working where I call a script in one module from another.
The modules are all in the same directory.
testModule.sikuli just has this:
from sikuli import *
def testFunc():
exit(1)
testImport.sikuli just has this:
import testModule
reload(testModule)
testModule.testFunc()
running testImport just yields:
[error] ImportError ( No module named testModule )
on the import testModule line.
I've tried various additions to testImport including:
myScriptPath="[my project path]"
if not myScriptPath in sys.path: sys.path.append(myScriptPath)
None of these seem to work.
I think the import just brings the new functions into the same module.
Try calling testFunc() instead of testModule.testFunc().
I've encountered the same issue. I have solved this problem using classes.
Try this code:
testModule.sikuli:
from sikuli import *
class test:
def testFunc(self):
exit(1)
testImport.sikuli:
import testModule
foo = testModule.test()
foo.testFunc()
This should work assuming your files are in the same folder (for example ./test/testImport.sikuli and ./test/testModule.sikuli)