-
Notifications
You must be signed in to change notification settings - Fork 936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Load local webpage from assets #349
Comments
#23 this maybe help ? |
No, I'm afraid that doesn't help. There is no solution there. |
I encountered this same challenge, and used a rather hack-y solution to the problem (at least until native functionality is added). For anyone else who comes across this same challenge, here's what worked for me:
final flutterWebViewPlugin = FlutterWebviewPlugin();
// Example call
void yourFunction() {
loadHTML("localview.html");
}
// Loads the given HTML file into the WebView
Future<String> loadHTML(String name) async {
var givenAsset = await rootBundle.loadString('lib/web/$name');
flutterWebViewPlugin
.reloadUrl(Uri.dataFromString(givenAsset, mimeType: 'text/html').toString());
return givenAsset;
} This solution is based on URI loading capability mentioned in #23. |
@Babelfisch If you can't use rootBUndle for whatever reason, it just looks like your URL is wrong... I have no idea where you can find the assets URL, but I did come across some code that lets you copy your local file into a new file which you do know URL of. Probably not something you want to do in production, but maybe it'll help you: Future<String> loadHTML(String name) async {
var givenAsset = rootBundle.loadString('lib/web/$name');
return givenAsset.then((String fileString) async {
String directory = (await getApplicationDocumentsDirectory()).path;
String filePath = '$directory/$name';
var file = new File('$filePath')
.writeAsString(fileString).then((File file) {
print(file.uri); // ← Your URL, use this wherever
flutterWebViewPlugin.reloadUrl(file.uri.toString());
});
});
} |
Thanks! But I’m afraid that embedded resources like images or stylesheets (also as assets) will not be loaded with this solution. Correct? |
can't display images or stylesheets |
Any movement on this? |
This path worked for me |
It works for IOS? |
Worked for me too. |
I try to load a local webpage from my assets:
But if I open the view in Android emulator I get the error message:
The webpage as file:///assets/web/loesungen.html could not be loaded because:
net::ERR_FILE_NOT_FOUND
The assets webpage is correct included in pubspec.yaml and I can load the webpage with:
I user Flutter 1.2.1, Dart 2.1.2 and flutter_webview_plugin 0.3.0
Any ideas why it’s not work?
The text was updated successfully, but these errors were encountered: