I wish to display image taken from the Camera.
My imports are -
import 'dart:html';
import 'dart:io';
import 'package:file/file.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
The code block with the issue
class _CollectdataState extends State<Collectdata> {
XFile imageFile;
_getFromCamera() async {
XFile? pickedFile = await ImagePicker().pickImage(
source: ImageSource.camera,
maxWidth: 1800,
maxHeight: 1800,
);
if (pickedFile != null) {
File imageFile = File(pickedFile.path);
}
}
The error here was
1.File needs 2 parameters- I am unsure on what the second parameter is.
Then I changed something around idr what exactly but the imports if I remember correctly and this was the new error-
File is not a function
I need to change it to a File inorder to upload on Firebase and show the picture using image.file()
Any help is highly appreciated!
Please remove
import 'dart:html';
If this package isn't used.
Both html and dart:io provide File. The error that you mentioned was for File from html.
Related
I want to write a file with flutter, that has got the same function as WORLD_READABLE in android. So i want tha this file can be read by all the applications.
More clear: i have an app A that write hello.txt i want that another app B can read this file. How i should set my file? There is a parameter of some function in the package path_provider?
I seen the package path_provider and File class, but i didn't find something.
I have already tried by access direct to file from app B, but is not world readable.
Something like this should work and make sure you use the latest path_provider dependency. getExternalStorageDirectory() does the trick!
//Write file from App 1
import 'dart:io';
import 'package:path_provider/path_provider.dart';
Future<void> writeToFile(String data) async {
final directory = await getExternalStorageDirectory();
final file = File('${directory.path}/hello.txt');
await file.writeAsString(data);
}
//Read the written file from App 2
import 'dart:io';
import 'package:path_provider/path_provider.dart';
Future<String> readFromFile() async {
final directory = await getExternalStorageDirectory();
final file = File('${directory.path}/hello.txt');
return file.readAsString();
}
Jr. flutter/firebase developer here. I'm trying to generate a thumbnail image from a video URL, save it to firebase, and return the URL to that image in Firebase but I'm in over my head.
I'm not sure if this should be done in-app or in a Firebase extension.
I've tried using the video_thumbnail package on pub.dev, but this is as far as I've gotten.
import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'dart:io';
import 'package:video_thumbnail/video_thumbnail.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
Future generateThumbnail() async {
final fileName = await VideoThumbnail.thumbnailFile(
video: "videoURL",
thumbnailPath: (await getTemporaryDirectory()).path,
imageFormat: ImageFormat.WEBP,
maxHeight:
900, // specify the height of the thumbnail, let the width auto-scaled to keep the source aspect ratio
quality: 66,
);
}
Any thoughts or advice would be appreciated.
I am trying to schedule a notification:but I got this error:
Unhandled Exception: Tried to get location before initializing timezone database
Although I initialized the Timezone in the init function:
tz.initializeTimeZones();
schedule code:
Future<void> scheduleNotifications(String title,int yy,int mm,int dd,int hh,int ii) async {
final loc_egypt = tz.getLocation('Africa/Cairo');
final tz.TZDateTime now = tz.TZDateTime.now(loc_egypt);
var schedule_30before=tz.TZDateTime(loc_egypt,yy,mm,dd,hh,ii).subtract(Duration(minutes: 30));
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
title,
"The Webinar will start after 30 minutes",
schedule_30before,
NotificationDetails(android: _androidNotificationDetails),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime);
}
In my case the problem was with invalid assets. I am working on a Flutter Web project, so I was doing:
import 'package:timezone/browser.dart' as tz;
void main() async {
await tz.initializeTimeZone();
// other stuff
}
After digging into the source code of timezone package, I discovered that it is initialising using a *.tzf file, which I wasn't including in my project's assets (rookie's mistake). So I edited my pubspec.yaml like this:
flutter:
assets:
- packages/timezone/data/latest.tzf
# - packages/timezone/data/latest_all.tzf <- for all the db variants
# - packages/timezone/data/latest_10y.tzf <- for 10y db variant
But there was still one more thing to do. As it turns out tz.initializeTimeZone() assumes that a default path is packages/timezone/data/latest.tzf BUT my Flutter Web project was putting all the assets in assets catalogue, so the actual path was assets/packages/timezone/data/latest.tzf and I just had to pass it as an argument to tz.initializeTimeZone('assets/packages/timezone/data/$dbVariant'). Of course you need to replace $dbVariant with the variant of your choice, the one that you included in your assets.
TL;DR
Include a timezone database in assets:
flutter:
assets:
- packages/timezone/data/latest.tzf
Initialise timezone with a full path to assets:
import 'package:timezone/browser.dart' as tz;
void main() async {
await tz.initializeTimeZone('assets/packages/timezone/data/latest.tzf');
// other stuff
}
You need to import the following packages:
import 'package:timezone/data/latest.dart' as tzl;
import 'package:timezone/standalone.dart' as tz;
Then, you can call for example something like:
ElevatedButton(
onPressed: ()async {
await scheduleNotifications("title", 2022, 9, 1, 1, 47);
},
child: Text("Notify Me!"))
Given you put tzl.initializeTimeZones(); in your initState().
Your issue is in using initializeTimeZones() of a package different than "package:timezone/data/latest.dart"
In the main() method of your app, verify your imports and call the init method
//Pick one of the three following imports :
//If you just want the latest
import 'package:timezone/data/latest.dart' as tz;
//If you use the 10y db variant
import 'package:timezone/data/latest_10y.dart' as tz;
//If you want to import all the db variants
import 'package:timezone/data/latest_all.dart' as tz;
void main() {
tz.initializeTimeZones();
}
In my case I created a future method and initialized it in the main() function of the main.dart file. This is my code:
import 'package:timezone/browser.dart' as tz;
Future<void> main() async {
initTimeZone();
// add other setup here
}
void initTimeZone(){
tz.initializeTimeZones();
}
Hope this helps!
I have some Uint8lists and I want to save them as jpg files.
Can anyone help?
By 'storage', do you mean write to a file? You don't really need "flutter" to do this. Just use the libraries provided by dart. Here is an example of downloading my gravatar which you can get as Uin8List and then saving it to a file.
import 'dart:io';
import 'dart:typed_data';
import 'package:http/http.dart' as http;
void main() {
http.get('https://www.gravatar.com/avatar/e944138e1114aefe4b08848a46465589').then((response) {
Uint8List bodyBytes = response.bodyBytes;
File('my_image.jpg').writeAsBytes(bodyBytes);
});
}
Here is a simple and short solution of your problem. Use this line in your code as I did:
"SettableMetadata(contentType: "image/jpeg"),"
Code:
if (kIsWeb) {
await ref.putData(
await imageFile.readAsBytes(),
SettableMetadata(contentType: "image/jpeg"),
);
url = await ref.getDownloadURL();
}
I basically want to show an image that I have in the assets folder onto the canvas.
import 'package:flutter/material.dart' as ui;
...
ui.Image img = ui.Image.asset("images/some_image.png");
ui.paintImage(canvas: canvas, image: img);
And have got the following error message when I tried to assign img to paintImage's image.
The argument type 'Image (C:\ABC\flutter\packages\flutter\lib\src\widgets\image.dart)' can't be assigned to the parameter type 'Image (C:\ABC\flutter\bin\cache\pkg\sky_engine\lib\ui\painting.dart)'.
I don't really know what could go wrong, I have seen other codes that had a similar approach with ui.Image.
Please advise.
There are two classes called Image in flutter, in different packages.
There's the Widget, which behaves like a widget and can be instantiated from an asset, from memory or from the network through its named constructors.
There's also the ui package Image which is used when painting at a lower level, for example in a CustomPainter. Since this version is in the ui package it's normally imported with the ui prefix like this:
import 'dart:ui' as ui;
Don't import material as ui! That will lead to a lot of confusion.
To make a widget, use the Image.asset constructor, passing the asset name.
To make a ui.Image from an asset use this snippet:
Future<ui.Image> load(String asset) async {
ByteData data = await rootBundle.load(asset);
ui.Codec codec = await ui.instantiateImageCodec(data.buffer.asUint8List());
ui.FrameInfo fi = await codec.getNextFrame();
return fi.image;
}