I am using the flutter_svg (version 0.10.3)package to load svg in my app. but it shows me an error saying that it is unable to load the resource.
here is my code
static final Map<Gender, String> _genderImage = {
Gender.female: 'assets/images/gender_female.svg',
Gender.other: 'assets/images/gender_other.svg',
Gender.male: 'assets/images/gender_male.svg'
};
Widget icon = Padding(
padding: EdgeInsets.only(left: _leftPadding(context)),
child: SvgPicture.asset(
_assetName,
height: _iconSize(context),
width: _iconSize(context),
));
this is the error:
Another exception was thrown: Unable to load asset: assets/images/gender_male.svg
Adding your assets to the pubspec.yaml is necessary, because Flutter uses the pubspec.yaml file, located at the root of your project, to identify assets required by an app.
Related
I have a flutter app that saves pdf files in a temporary directory then the user can press a button to delete these files, in Android everything works fine but in iOS I get this error:
Unhandled Exception: FileSystemException: Deletion failed, path =
'/var/mobile/Containers/Data/Application/75048CC8-24E7-4BA3-B748-5603D4F74F60/Library/Caches'
(OS Error: Operation not permitted, errno = 1)
The weird thing is also that if you refresh the app you can see that the files have been deleted so actually it works but gives error and blocks the app.
My code for deleting is this:
CupertinoDialogAction(
child: Text("Ok"),
onPressed: () async {
Directory dir = await getTemporaryDirectory();
dir.deleteSync(recursive: true);
dir.create();
Navigator.pop(context);
openFinishDialog();
}
),
Also this error shows only in physical devices and not on simulators.
I used the dart package path_provider 2.0.8
Okay I solved the problem by creating a directory inside the TemporaryDirectory and deleting that instead of the TemporaryDirectory, here's the code for creating:
var tempDir = await getTemporaryDirectory();
var tempDirPath = tempDir.path;
final myAppPath = '$tempDirPath/my_app';
final res = await Directory(myAppPath).create(recursive: true);
and here's the code for deleting:
Directory dir = await getTemporaryDirectory();
Directory myDir = Directory(dir.path + "/my_app");
myDir.deleteSync(recursive: true);
myDir.create();
I hope this is useful for someone else, if somebody has a better way to solve this issue please answer!
Creating a Directory inside the directory is the solution, you can also use getApplicationDocumentsDirectory() instead of getTemporaryDirectory() for android and ios.
I am using the following plugin to share files in my flutter app:
[Flutter Share Plugin][1]
Here's how I use it my code:
new IconButton(icon: new Icon(FontAwesomeIcons.download, size: 35),
onPressed: () {
final RenderBox box = context.findRenderObject();
Share.image(path: "file:../assets/lake.jpg",mimeType: ShareType.TYPE_IMAGE,title: "title",text:"text").share(
sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);
},
),"SHARE"),
Notice the path is set to ../assets/lake.jpg. This is because this dart file resides under lib/pages folder whereas the picture that I am trying to access is under assets folder.
Here's the screenshot:
When I click on the share button, the share box comes up and when I try to share the image via whatsapp for example, it even asks me "Share with Mr. X"?
However, it does not share anything and does not give me any sort of error message.
I'm working on a mobile app--I'm using flutter and it needs to view documents in my assets. I used the pdfviewer and it works perfectly fine. But with openfile, it does not.
I used the flutter package,
https://pub.dartlang.org/packages/flutter_pdf_viewer and it opens the pdf file but with this package, https://pub.dartlang.org/packages/open_file it won't open any files.
This is how i call them:
onPressed: () => FlutterPdfViewer.loadAsset("assets/files/sample.pdf"),
onPressed: () => OpenFile.open('assets/files/samplefile.docx'),
I don't know any more methods to open files in flutter. Am I using the open_file the wrong way? I hope you can help me figure this out.
Future<File> pdfAsset() async {
Directory tempDir = await getTemporaryDirectory();
File tempFile = File('${tempDir.path}/set_any_name.pdf');
ByteData bd = await rootBundle.load('assets/en/legal_notes.pdf');
await tempFile.writeAsBytes(bd.buffer.asUint8List(), flush: true);
return tempFile;
}
pdfAsset().then((file){OpenFile.open(file.path);});
Hi first copy the asset to temp file then open the file with OpenFile.
It works for me.
You need to update your pubspec.yaml with full path of the asset.
flutter:
assets:
- assets/files/samplefile.docx
I ran flutter upgrade today, and now I am getting an error that says-
[dart] The named parameter 'child' isn't defined.
The project is newly created and the default code is untouched, but it still has the same Error:
Clean the project cache by running
flutter clean cache
Then invalidate caches / restart Android Studio or VS Code.
Try Restarting your Analysis Dart Server.
At the bottom of Android Studio click on the Dart Analysis tab
Click on the Restart icon.
In my case it happens when I name the widget with the same name of a flutter component, like so:
class OutlineButton extends StatelessWidget {
#override
Widget build(BuildContext context) {
return OutlineButton(
child: Text('+R\$ 5'),
onPressed: () {},
borderSide: BorderSide(color: Colors.grey),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),
),
);
}
}
You need to change the name of the created component with a different name, for example:
class CustomOutlineButton extends StatelessWidget {
#override
Widget build(BuildContext context) {
return OutlineButton(
child: Text('+R\$ 5'),
onPressed: () {},
borderSide: BorderSide(color: Colors.grey),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),
),
);
}
}
Go to file Settings
Search Flutter (in the search bar)
Provide Flutter SDK path
Actually, I reinstalled the flutter SDK to solve the problem. After a few days, the same error occurred, but then I Started hovering over the red line, after a minute, the error was solved automatically. I guess the SDK connects with internet to check for all the libraries and syntax stuff once we open the IDE.
It happens because of the old cache. Just clean the cache with the following command
flutter clean cache
I just had the same issue. It looks like I changed a flutter lib file by mistake.
To fix this, without reinstalling Flutter SDK:
go to your Flutter SDK installation path
open terminal and type: git status
it will show the modified files in red (ex: modified: packages/flutter/lib/src/widgets/basic.dart
revert modifications file using: git checkout FILE_PATH (ex: git checkout packages/flutter/lib/src/widgets/basic.dart)
The solution is -
Change the name of the class in which you are using the Center widget because, according to the standards of flutter you cannot write the name of the class same as the widget you are using inside that class. If you do so, then you cannot be able to make child or children inside that widget.
Also, the solution of Felipe Augusto is correct.
I tried adding an image with a SVG source to my flutter application.
new AssetImage("assets/images/candle.svg"))
But I didn't get any visual feedback.
How can I render an SVG picture in Flutter?
Fonts are a great option for a lot of cases.
I've been working on a library to render SVGs on a canvas, available here:
https://github.com/dnfield/flutter_svg
The API as of right now would look something like
new SvgPicture.asset('asset_name.svg')
to render asset_name.svg (sized to its parent, e.g. a SizedBox). You can also specify a color and blendMode for tinting the asset..
It's now available on pub and works with a minimum of Flutter version 0.3.6. It handles a bunch of cases but not everything - see the GitHub repo for updates and to file issues.
The original GitHub issue referenced by Colin Jackson is really not meant to be primarily focused on SVG in Flutter. I opened another issue here for that: https://github.com/flutter/flutter/issues/15501
Flutter does not currently support SVG. Follow issue 1831 for updates.
If you absolutely need vector drawing you can see the Flutter Logo widget as an example of how to draw using the Canvas API, or rasterize your image on the native side and pass it to Flutter as a bitmap, but for now your best bet is probably to embed high-resolution rasterized asset images.
Developers from the Flutter community created a lib to handle svg files. We can use it as
new SvgPicture.asset(
'assets/images/candle.svg',
height: 20.0,
width: 20.0,
allowDrawingOutsideViewBox: true,
),
I found a small example of SVG implementation here.
The work around for now is use fonts
https://icomoon.io/
fonts:
- family: icomoon
fonts:
- asset: assets/fonts/icomoon.ttf
Useage
static const IconData TabBarHome= const IconData(0xe906, fontFamily: 'icomoon');
static const IconData TabBarExplore= const IconData(0xe902, fontFamily: 'icomoon');
Replace the ### eg (906)
You can follow the below steps
- visit http://fluttericon.com
- upload your SVG icons
- click on download button
- you will get two files
1. iconname.dart 2. iconname.ttf font file
- use this file in flutter & import iconname.dart
step 1. add dependency in your pubspec.yaml
dependencies:
flutter_svg: any
the Advantage of version any is you can use in any SDK version
but dependencies version is most important so add suitable version for your dependency.
step 2. import the flutter svg package in your app
import 'package:flutter_svg/flutter_svg.dart';
step 3. just use like below
SvgPicture.asset(
'assets/images/bm-icon1.svg',
width: 18.0,
height: 18.0,
),
You can use this library for rendering SVG Images - https://pub.dev/packages/flutter_svg
Example -
Container(
child: SvgPicture.asset("assets/images/yourImage.svg")
)
I solved similar problem by converting the svg images to ttf at fluttericon.com.
These are the steps involved.
Keep all your svg files in a folder.
Upload all of them to fluttericon by using the uploader there.
You can select all of them and drag and drop at the spot.
Give your icons a dart class name at the top: like, Svgicons
Select all your icons and press Download.
It will download a zip file with a dart file and fonts folder which contains Svgicons.ttf.
copy the fonts folder to your project folder and refer to it as an asset in pubspec.yaml.
fonts:
- family: Svgicons
fonts:
- asset: fonts/Svgicons.ttf
import the svgicons_icons.dart where you are going to use the images.
Sample:
import '../svgicons_icons.dart';
import 'social_media.dart';
class SocialMediaRepository {
List<SocialMedia> getSocialMedia = [
SocialMedia(id: 1, title: "Facebook", iconAsset:Svgicons.facebook, isFavorite: false),
SocialMedia(id: 2, title: "Instagram", iconAsset: Svgicons.instagram, isFavorite: false),
SocialMedia(id: 3, title: "LinkedIn", iconAsset: Svgicons.linkedin, isFavorite: false),
....
Here iconAsset is of type IconData which used to be a String referring to the svg image originally.
Display the ttf by using
...
Icon(item.iconAsset),
SizedBox(width: 10),
Text(item.title),
],
),
where item is the object containing IconData.
Now you can remove the reference to svg images from the project.
You can use flare to create animations and just import .flr as an asset
import 'package:flare_flutter/flare_actor.dart';
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return new FlareActor("assets/Filip.flr", alignment:Alignment.center, fit:BoxFit.contain, animation:"idle");
}
}
visit flare_flutter https://pub.dev/packages/flare_flutter
Of possible interest: There's a newer Flutter SVG library called jovial_svg. I wrote it to handle a more robust subset of SVG than what I was able to find on pub.dev at the time. While I was at it, I also made a binary format that SVG assets can be compiled to. It's a little more compact, and about 10x faster to read, so it's a good choice for assets you package with your application.
The library is at https://pub.dev/packages/jovial_svg
flutter support special type of svg
if have style tag then not load svg image in flutter
flutter support inline styling in svg
Svg in flutter https://pub.dev/packages/flutter_svg
add package in pubspec.yaml
dependencies:
flutter_svg: any
assets:
-assets/images/
to insert svg.
SvgPicture.asset('assets/images/password-icon.svg',
width: 24, height: 29.2),