OS Error: No such file or directory, errno = 2

13,387

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');
}
Share:
13,387
TWH
Author by

TWH

Updated on June 21, 2022

Comments

  • TWH
    TWH almost 2 years

    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.