FlutterError (Unable to load asset: assets/chat1 .txt) - flutter

I know I placed my text file at the location assets\chat1.txt, and my code also works with other txts but somehow I always get the following error:
Exception has occurred.
FlutterError (Unable to load asset: assets/chat1.txt)
my Code:
Future<String> getFileData(String path) async {
return await rootBundle.loadString(path);
}
pubspec.yaml
flutter:
assets:
- assets/

Add the full path in pubspec.yaml file and you should consider the indentation for assets:
flutter:
assets:
- assets/chat1.txt

Related

Flutter audioplayers: ^2.0.0 error Unhandled Exception: Unable to load asset: assets//data/user/0/com.xxxx.xxxx/cache/flip.mp3

I'm trying to use audioplayers: ^2.0.0,but i got this error, how to solve it?please!
pubspec.yaml
flutter:
uses-material-design: true
assets:
- assets/audio/
Code
Future setAudio() async {
AudioPlayer audioPlayer = AudioPlayer();
audioPlayer.setReleaseMode(ReleaseMode.loop);
AudioCache audioCache = AudioCache(prefix: 'assets/audio/');
final url = await audioCache.load('flip.mp3');
audioPlayer.setSourceAsset(url.path);
}
You shouldn't use the full path in setSourceAsset, and since you've already set a prefix for the cache you kust have to write the filename:
audioPlayer.setSourceAsset('flip.mp3');

Exception ,trying to run a picture on the app

When I try to let my flutter app show a picture(it's asset image),it throws the exception :
"Exception: Gradle task assembly debug failed with exit code 1".
What should I do in such a case ?
import 'package:flutter/material.dart';
void main()
{
runApp(Center(
child: Image(
image:AssetImage('images/mont.jpg'),
)
));
}
the pubspec.yaml file is :
flutter:
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- images/mont.jpg
- images/a_dot_ham.jpg

Flutter not load assets

I have a problem with flutter the assets below are not displayed inside the widget, it tells me unable to load assetss how can I do to fix this?
Dart Code:
final cookingEvent = Event(
imagePath: "assets/event_images/granite_cooking_class.jpeg",
title: "Basket",
description: "",
location: "Ferrara(FE)",
duration: "1ms",
punchLine1: "Partecipa!",
punchLine2: "nuova partita di calcio",categoryIds: [0, 2],
galleryImages: ["assets/event_images/cooking_1.jpeg", "assets/event_images/cooking_2.jpeg", "assets/event_images/cooking_3.jpeg"]);
Flutter Exception:
r: "assets/event_images/5_km_downtown_run.jpeg", scale: 1.0)
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════
flutter: Another exception was thrown: Unable to load asset: assets/event_images/granite_cooking_class.jpeg
flutter: Another exception was thrown: Unable to load asset: assets/event_images/music_concert.jpeg
flutter: Another exception was thrown: Unable to load asset: assets/event_images/golf_competition.jpeg
Pubspec.yaml:
assets:
- assets/guest_images/
- assets/event_images/
- assets/fonts/
fonts:
- family: Montserrat
fonts:
weight: 700
Make sure that the path of your image is correct.
Then check for tab spaces.
make sure to run flutter pub get command.
Then restart your app or hot reload your app.

Flutter 1.0 - Unable to load text asset

I'm having some trouble loading text assets in flutter (1.0).
This is the current code attempting to read the asset.
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
Future<String> getFileData(String path) async {
return await rootBundle.loadString(path);
}
Future<File> get _localFile async {
final path = await _localPath;
final file = File('$path/toon_test_3.json');
bool exists = file.existsSync();
if(exists){
return file;
} else {
// Load the default file
final newfile = await getFileData('toonRepo/data.json');
return file.writeAsString(newfile);
}
}
loadString will failing popping up the following:
Could not load source 'dart:core/runtime/libobject_patch.dart': <source not available>.
If execution continues the following exception is raised:
Exception has occurred.
FlutterError (Unable to load asset: toonRepo/data.json)
I've tried a lot of solutions here that revolve around the asset section in pubspec.yaml
name: hello_world
description: A new Flutter project.
version: 1.0.0+1
environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0"
dependencies:
path_provider: ^0.4.1
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
english_words: ^3.1.0
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
assets:
- toonRepo/
uses-material-design: true
Am I going wrong somewhere very silly?
Cheers for any pointers you have.
Project Structure
Here is a temporary copy of the code if you want to take a peek.
Github
And here's a stack trace
[VERBOSE-2:shell.cc(184)] Dart Error: Unhandled exception:
Unable to load asset: toonRepo/data.json
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
<asynchronous suspension>
#1 AssetBundle.loadString (package:flutter/src/services/asset_bundle.dart:67:33)
<asynchronous suspension>
#2 CachingAssetBundle.loadString.<anonymous closure> (package:flutter/src/services/asset_bundle.dart:162:56)
#3 __InternalLinkedHashMap&_HashVMBase&MapMixin&_LinkedHashMapMixin.putIfAbsent (dart:collection/runtime/libcompact_hash.dart:284:23)
The asset access works fine with the pubspec.yaml in your question.
new Text("Name: " + toon.info.name)),
fails because toon is null because it never got a value assigned.
You could use new Text("Name: " + (toon?.info?.name ?? 'foo')), to work around the exception.
If you add
widget.storage.getFileData('toonRepo/data.json').then((f) => print(f));
to _FlutterDemoState.initState(), you'll see that reading the asset works just fine.

Flutter play custom sound using audioplayers 0.7.7?

pubspec.yaml
flutter:
uses-material-design: true
assets:
- assets/Images/1.png
- assets/Images/MP3.mp3
Test.dart
Widget localAsset() {
return _tab([
Text("Click to play"),
_btn('Play', () => audioCache.play('assets\Images\MP3.mp3')),
]);
}
I am new to flutter, for my applications i want play two sounds mode(background sound ,button action sound), after referred from flutter package i have changed code like as above , when i used this widget in my material,i am getting below error,
E/flutter ( 2750): [ERROR:flutter/shell/common/shell.cc(181)] Dart Error: Unhandled exception:
E/flutter ( 2750): Unable to load asset: assets/assetsImagesMP3.mp3
E/flutter ( 2750): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
Backslash are Windows-specific. Use slashes instead. Android is Unix-based and so is iOS
audioCache.play('assets/Images/MP3.mp3')