8

I have trouble loading a html file stored in my assets folder. I've been searching for 2 days and I can't seem to find out why.

I've made sure to include it in pubspec.yaml as well, with the proper indentations and all - builds fine. The image assets I loaded in pubspec.yaml loads no problem so I know everything should be okay with that part.

I've also tried to test it with a .txt file following the exact sample code here: https://api.flutter.dev/flutter/dart-io/File-class.html - from "Reading from a file" as a stream section. It gives the same error.

This is what I'm trying to do:

// Tried this with a test .txt file too ('assets/html/test.txt')
File file = new File('assets/html/emaiL_bank.html');

Stream<List<int>> inputStream = file.openRead();
inputStream
    .transform(utf8.decoder)
    .transform(new LineSplitter())
    .forEach((l) => print('testing Print: $l'));

This is a section of my pubspec.yaml:

flutter:
  assets:
    - assets/images/ic_splashscreen.jpg
    - assets/images/at.png
    - assets/images/lock.png
    - assets/html/emaiL.html
    - assets/html/emaiL_bank.html
    - assets/html/test.txt

..and I get OS Error: No such file or directory, errno = 2 when it's trying to do file.openRead()

Tried flutter clean, restarting IDE, rebuild - nothing seems to have any effect.

Any help/clue would be much appreciated.

TWH
  • 185
  • 1
  • 2
  • 11

1 Answers1

4

please use rootBundle to load assets https://flutter.dev/docs/development/ui/assets-and-images

import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;

Future<String> loadAsset() async {
  return await rootBundle.loadString('assets/config.json');
}

If you are trying load html file for webview, please reference this https://inducesmile.com/google-flutter/how-load-a-local-html-file-in-flutter-webview/

Future<String> _loadLocalHTML() async {
  return await rootBundle.loadString('assets/html_code.html');
}

code from reference document

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import 'dart:async';

class LoadHTMLFileToWEbView extends StatefulWidget {
  @override
  _LoadHTMLFileToWEbViewState createState() => _LoadHTMLFileToWEbViewState();
}

class _LoadHTMLFileToWEbViewState extends State<LoadHTMLFileToWEbView> {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder<String>(
      future: _loadLocalHTML(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return WebviewScaffold(
            appBar: AppBar(title: Text("Load HTM file in WebView")),
            withJavascript: true,
            appCacheEnabled: true,
            url: new Uri.dataFromString(snapshot.data, mimeType: 'text/html')
                .toString(),
          );
        } else if (snapshot.hasError) {
          return Scaffold(
            body: Center(
              child: Text("${snapshot.error}"),
            ),
          );
        }
        return Scaffold(
          body: Center(child: CircularProgressIndicator()),
        );
      },
    );
  }
}

Future<String> _loadLocalHTML() async {
  return await rootBundle.loadString('assets/html_code.html');
}
chunhunghan
  • 51,087
  • 5
  • 102
  • 120
  • Thank you. This seems to work. Btw I'm not trying to load it into a webview. I'm trying to read it as a string which then need to be processed and sent to an API afterwards. Also, out of curiosity. why must one use rootBundle to load from asset? What sort of path would be loadable through the File class? – TWH Sep 11 '19 at 08:54
  • full path looks like /scard/0/your_andorid_dir/your_asset_file. it's impossible to know correct full path of every android type. – chunhunghan Sep 11 '19 at 08:59
  • I see. Thanks for your help, kind sir. – TWH Sep 11 '19 at 09:20