Rank 2: Process
Hi I am trying to access a asset in my dart function but I don't know how to set the path properly.
Votes
0
Replies
7
Participants
Unknown
Messages
8
Rank 2: Process
Hi I am trying to access a asset in my dart function but I don't know how to set the path properly.
Rank 3: Container
Would it be possible to share the code to see how your imports and folder structure are doing?
Rank 3: Container
I have some functions in dart, what I needed to do was import them from the dart project name:
pubspec.yaml:
name: my_function
in the archives:
import 'package:my_function/modules/model/any_files.dart’;
Rank 2: Process
Rank 2: Process
For example rootBundle.load("lib/example.txt'')
Rank 2: Process
Rank 1: Thread
import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;
Future loadAsset() async {
return await rootBundle.loadString('assets/my_text.txt');
}
Rank 1: Thread
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 loadAsset(BuildContext context) async {
return await DefaultAssetBundle.of(context).loadString('assets/my_text.txt');
}