2

I'm trying to read a file in a flutter app.

The file exists in:

/assets/timezone/data/2019b.tzf

My pubspec contains:

- assets/timezone/data/2019b.tzf

My dart code calls:

rootBundle.load('packages/timezone/data/2019b.tzf');

Flutter returns the following:

Exception has occurred.
FlutterError (Unable to load asset: assets/timezone/data/2019b.tzf)

Unit Test

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

Directory tree:

├── assets
│   └── timezone
│       └── data
│           ├── 2019b.tzf
├── lib
├── pubspec.yaml
├── test
│   └── region_test.dart

2 Answers 2

1

I think you can use rootBundle.load('assets/timezone/data/2019b.tzf') only in your app.

In tests you can do this:

final bytes = File('assets/timezone/data/2019b.tzf').readAsBytesSync();
final byteData = ByteData.view(bytes.buffer);
Sign up to request clarification or add additional context in comments.

Comments

1

In the pubspec.yaml, Replace '2019b.tzf' with latest database version (it's currently '2020d.tzf'). also you can use 'latest.tzf'

flutter:
  ..
  ..
  assets:
    ..
    ..
    - assets/timezone/data/latest.tzf

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.