Flutter file word readable - flutter

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();
}

Related

Flutter: get project root's path

How can I get the project's root folder in Flutter? Basically what is the equivalent of python's os.path.dirname(os.path.abspath(__file__))?
you can use Directory.current to get your projects root directory. This is the directory where your pubspec.yaml is located. Checkout an example below.
import 'dart:io';
main() {
Directory current = Directory.current;
print(current);
print(current.path);
}
Im not sure what you mean but if you want app directory you can use this package
path_provider
import 'package:path_provider/path_provider.dart';
Future<Directory> getAppDirectory() async {
final directory = await getApplicationDocumentsDirectory();
return directory;
}

Why can I not change from XFile to File?

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.

Tried to get location before initializing timezone database

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!

Is there any option to hide/ remove .hive files?

When I used hive as local db in flutter, .hive files are generated in user's app-directory. Is there any way to hide or remove these files from the app-directory?
You can remove file which contains the box and closes the box by using this method.
deleteFromDisk
Use path_provider to store hive file in getApplicationSupportDirectory
Future<void> init() async {
final path = await getApplicationSupportDirectory();
await Hive.initFlutter();
Hive.registerAdapter(UserAdapter());
await Hive.openBox<User>(HiveConst.userDataBox, path: path.path);
}

Save a memory image (such as Uint8list) as image file in flutter

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();
}