i have read https://flutter.dev/docs/development/ui/assets-and-images#asset-images-in-package-dependencies and Flutter - Read text file from assets and applied all there was but my code still doesn't work....
i opened a new project for this, in the main folder i created assets and a file :
pwd
/home/bboett/AndroidStudioProjects/examen_companion
bboett@hayate:~/AndroidStudioProjects/examen_companion$ ls -l assets/
insgesamt 4
-rw-r--r-- 1 bboett bboett 10 19. Mai 15:14 test.txt
bboett@hayate:~/AndroidStudioProjects/examen_companion$ cat assets/test.txt
Hello!!
then, not trusting android studio, i checked with vi, that in pubspec.yaml everything was ok:
the file ends with:
flutter:
uses-material-design: true
assets:
- assets/
i replaced the spaces, with 2 spaces before uses and assets: and 4 before - assets...
in the _MyHomePageState class i changed :
@override
Widget build(BuildContext context)
{
AssetBundle bundle = DefaultAssetBundle.of(context);
return FutureBuilder<String>(
future: bundle.loadString("assets/test.txt"),
builder: (context, AsyncSnapshot<String> snapshot)
{
if (snapshot.hasData) { return Text(snapshot.data.toString()); }
else { return CircularProgressIndicator(); }
}
);
}
doesn't work.... i never come out of the progress indicator.... so directly in the main i added :
void main() async {
print(await rootBundle.loadString("assets/test.txt"));
runApp(MyApp());
}
and that crashes with :
Launching lib/main.dart on Linux in debug mode...
Building Linux application...
Debug service listening on ws://127.0.0.1:41355/L-ev6_eNIlI=/ws
Syncing files to device Linux...
[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:222:39)
#1 AssetBundle.loadString (package:flutter/src/services/asset_bundle.dart:68:33)
#2 CachingAssetBundle.loadString.<anonymous closure> (package:flutter/src/services/asset_bundle.dart:165:56)
#3 _LinkedHashMapMixin.putIfAbsent (dart:collection-patch/compact_hash.dart:311:23)
#4 CachingAssetBundle.loadString (package:flutter/src/services/asset_bundle.dart:165:27)
#5 main (package:examen_companion/main.dart:9:26)
i did after changing anything prior to the run a flutter clean.....
version is :
flutter upgrade
Flutter is already up to date on channel beta
Flutter 2.2.0-10.3.pre • channel beta • https://github.com/flutter/flutter.git
Framework • revision 06e2fd6357 (vor 11 Tagen) • 2021-05-08 11:28:22 -0700
Engine • revision a123e75c60
Tools • Dart 2.13.0 (build 2.13.0-211.14.beta)
flutter doctor run ok too, oh and i have the same error on linux or android, so its not the device....
so i am pretty clueless on how to get this work, since i have the impression to have followed documentation and previous help :(
BTW i thought that flutter was now null, safe... anyway, how do i get this to work?
thanks in advance
[edit]: even stranger.... i replaced
//future: bundle.loadString("assets/test.txt"),
future: bundle.loadString('AssetManifest.json'),
and got :
flutter: {"assets/test.txt":["assets/test.txt"],"packages/cupertino_icons/assets/CupertinoIcons.ttf":["packages/cupertino_icons/assets/CupertinoIcons.ttf"]}
so the file is there??? why can't i open/get it??
[ed2]: ok, i don't get it..... i tryed this directly in main:
print(await rootBundle.loadString('AssetManifest.json'));
and that crashed too with the null exception....
I tried your code, and it works for me. Assets can be tricky. Sometimes you will need to restart the app completely after adding a new asset, or uninstall the app completely before running it again. You can also try running flutter clean
.
An advice is to add if(snapshot.hasError) print(snapshot.error);
before the else in the FutureBuilder
just to see what the error is if it still does not work.
The reason you got an error about the null check when printing in main is because you need to add WidgetsFlutterBinding.ensureInitialized();
before using the root bundle to ensure that you have an instance of WidgetsBinding
.