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

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

Related

Flutter file word readable

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

flutter path_provider could not read downloaded files

I am using path_provider on flutter and would like to build Download file(mp4&jpg, from aws S3) and play it. I succeeded download and confirmed file is downloaded.(on ios)
But, when access the files using path_provider, raised error like below.
Unable to load asset: /Users/username/Library/Developer/CoreSimulator/Devices/B78BC67E-B127-4DD0-8A42-965EE645157B/data/Containers
the saving method is below
Future<File> _getFile(String filename) async {
final dir = await getApplicationDocumentsDirectory();
return File("${dir.path}/$filename");
}
final file = await _getFile("filename");
file.writeAsBytes(Bytes);
and the load method is below
final directory = await getApplicationDocumentsDirectory();
.
.
.
Image(image: AssetImage('${directory.path}/filename')) // /Users/username/Library/Developer/CoreSimulator/Devices/B78BC67E-B127-4DD0-8A42-965EE645157B/data/Containers/Data/Application/46EF23F5-840C-47D4-8314-B97CDEE2198C/Documents
.
.
.
Actually I need to use FileImage instead of Asset.
Thanks for GrahamD

How do you download an image from the internet onto a file in Flutter?

I'm making a Flutter project where an image is displayed in listview and the user can share, favourite or download the image. I'm stuck on the downloading part. Is it possible to save an image file onto the phone's storage for offline use?
What I want to happen is I can create and write an image file
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
Future<File> get _localFile async {
final path = await _localPath;
return File('$path/$title.txt');
}
// Write an image by obtaining an image through a URL
and then be able to access it through
Image.file('$path/$title.txt')

Flutter SharedPreferences resetting the data

I have this code in flutter using SharedPreferences to store data:
Future<bool> setUserStatus(String userStatus) async{
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('userStatus', 'active');
return true;
}
Is it possible to use this same setUserStatus in another file, which will get this main.dart imported to it, and change the SharedPreferences data to something else based on the actions taken in the other file
Do this,
await setUserStatus( status );
if you don't want to wait for the future to complete just remove await from the begining.
Calling prefs.clear()will erase all the preferences set on the device. so I would suggest not to use that here. If you want to clear a particular preference just use
prefs.remove(key) or prefs.setString(key,null)

How to update value inside shared preferences in flutter

actually when saving data inside shared preferences.. I am using this code
add() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('data', "ok");
}
but, is there a way to update the value of data for example I want to change ok into fine
because when I try to re-save my data using that code... and call it using prefs.getString('data'); it always shows the old data not the update one
Just reassign it again
prefs.setString('data', "fine");
//shared-preferences
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('profileImg', data1['imagePath']);
prefs.setString('un', data1['username']);