从资产读取文本文件

我有一个文本文件(. txt) ,我想成为一个资产,我可以在稍后扫描。

在 pubspec.yaml 上,我确保:

flutter:
assets:
- res/my_file.txt

该文件位于我创建的 res/文件夹中,与 lib/android/ios/处于同一级别

我试图从一个自定义类中读取文件,而不是从一个小部件中。

根据文件,我将使用这个导入:

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

然后像这样读:

/// Assumes the given path is a text-file-asset.
Future<String> getFileData(String path) async {
return await rootBundle.loadString(path);
}

为了得到实际的数据,需要:

String data = await getFileData(fileName);

但是,当我使用像 'assets/res/my_file.txt'这样的 fileName时,会得到一个错误: Unable to load asset: assets/res/my_file.txt

同样值得注意的是,我正在尝试从单元测试中完成这项工作。有什么好主意吗?谢谢!

96155 次浏览

The folder name "assets" isn't magically added. Update your pubspec.yaml to include the full path to the asset.

flutter:
assets:
- assets/res/my_file.txt

Here is a fuller answer for future visitors.

Create an assets folder

Create an assets folder in your project's root folder. In Android Studio you can right click the Project outline and go to New > Directory.

assets

You can create another subfolder for text files in assets if you like. But if you do, you have to include the relative path in pubspec.yaml. See below.

Add your text file to the new folder

You can just copy your text file into the assets directory. The relative path of my_file.txt, for example, would be assets/my_file.txt.

Register the assets folder in pubspec.yaml

Open the pubspec.yaml file that is in the root of your project.

Add an assets subsection to the flutter section like this:

flutter:
assets:
- assets/my_file.txt

If you have multiple files that you want to include, then you can leave off the file name and just use the directory name (include the final /):

flutter:
assets:
- assets/

Get the text in code

You can use the global rootBundle to get the text file asset:

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


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

Or if you have the BuildContext (inside a widget) you can use DefaultAssetBundle. This is recommended because it allows switching asset bundles at runtime, which is useful for multilingual assets.

Future<String> loadAsset(BuildContext context) async {
return await DefaultAssetBundle.of(context).loadString('assets/my_text.txt');
}

See also

In my opinion, in order to load a js file into a flutter, you should consider it as a text file and load it properly. So, you need to add the file to assets folder, add into a pubspec file, then load it. read the full answer here

Second, you used evalJavascript. this function can be used in many different situations. but it will work only if you have a view panel.

Check below example:

import 'dart:io';


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


main() async {
String jsCode = await rootBundle.loadString('assets/javascript.js');


runApp(new MaterialApp(
home: LunchWebView(jsCode),
));
}


class LunchWebView extends StatelessWidget {
final String text;
LunchWebView(this.text);


@override
Widget build(BuildContext context) {
final FlutterWebviewPlugin flutterWebviewPlugin = FlutterWebviewPlugin();
flutterWebviewPlugin.launch('https://www.google.com');
flutterWebviewPlugin.evalJavascript(text);
return Container();
}
}