我正在尝试在Flutter应用中读取文件。
文件存在于:
/assets/timezone/data/2019b.tzf
我的pubspec包含:
- assets/timezone/data/2019b.tzf
我的飞镖代码调用:
rootBundle.load('packages/timezone/data/2019b.tzf');
Flutter返回以下内容:
Exception has occurred.
FlutterError (Unable to load asset: assets/timezone/data/2019b.tzf)
单元测试
import 'dart:typed_data';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group("Test Load", () {
test("load", () async {
try {
ByteData byteData =
await rootBundle.load('assets/timezone/data/2019b.tzf');
print("loaded successfully size: ${byteData.buffer.lengthInBytes}");
} catch (e) {
print(e);
}
print("done");
});
});
}*
pubspec.yaml
name: regions
description: A new Flutter project.
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:
- assets/timezone/data/2019b.tzf
目录树:
├── assets
│ └── timezone
│ └── data
│ ├── 2019b.tzf
├── lib
├── pubspec.yaml
├── test
│ └── region_test.dart
答案 0 :(得分:0)
我认为您只能在您的应用中使用rootBundle.load('assets/timezone/data/2019b.tzf')
。
在测试中,您可以执行以下操作:
final bytes = File('assets/timezone/data/2019b.tzf').readAsBytesSync();
final byteData = ByteData.view(bytes.buffer);
答案 1 :(得分:0)
在pubspec.yaml中,将“ 2019b.tzf ”替换为最新的数据库版本(当前为“ 2020d.tzf ”)。 您也可以使用“ latest.tzf”
flutter:
..
..
assets:
..
..
- assets/timezone/data/latest.tzf