jonasroussel / dart_jsonwebtoken

A dart implementation of the famous javascript library 'jsonwebtoken'
MIT License
87 stars 29 forks source link

FileSystemException: Cannot open file, path #36

Closed Zechst closed 1 year ago

Zechst commented 1 year ago

Hi,

I am facing an issue with the pem file. Following the example provided, I've created a privatekey.pem file and placed it inside the assets folder.

Here's my code: final pem = File('assets/rsa_private_key.pem').readAsStringSync(); final RSAPrivateKey privateKey = RSAPrivateKey(pem);

I've also specified the file in pubspec.yaml under assets

assets:
    - assets/
    - assets/images/
    - assets/icons/
    - assets/rsa_private_key.pem

However, for some reason caught an exception ERROR: FileSystemException: Cannot open file, path = 'assets/rsa_private_key.pem' (OS Error: No such file or directory, errno = 2) Somehow File was not able to read the rsa_private_key.pem file

jonasroussel commented 1 year ago

Hello,

In flutter you can't read a file directly, you need to use rootBundle module. Here is an example :

https://stackoverflow.com/questions/44816042/flutter-read-text-file-from-assets

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

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