Flutter : How to persist File content upon application uninstall? - android

I created a class to store a data in a file, so that I can refer the data again the next time I open the apps.
Sample code are as follows:
static Future<String> get localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
static Future<File> get localFile async {
final prefs = await SharedPreferences.getInstance();
final String fileName = 'file01';
final path = await localPath;
return File('$path/$fileName.txt');
}
static Future<String> readContent() async {
try {
final file = await localFile;
String contents = await file.readAsString();
return contents;
} catch (e) {
return "null";
}
}
Write method
static Future<File> writeContent(String content) async {
final file = await localFile;
var result = readContent();
return file.writeAsString('$content', mode: FileMode.append);
}
writeContent() method will be triggered on some events in the apps. It will create a file if it does not existed, and append a new data if it does.
After I uninstalled the apps, the readContent() gives me "null" response. Any idea on how to persist the File?

As far as I'm aware, uninstalling an app deletes all files and directories associated with it.
I would store the files in a database and, in case you need them locally, download them when the user first opens the app.

Related

Is it possible to delete an externally scoped image file from the phone using Flutter?

I want to select a photo through the OS gallery picker, obtain the path or unique identifier, then delete that photo from the phone entirely.
This is not a file that is local or within the Flutter app itself.
Using an Android emulator, when I get the path of the file it is a cached version internal to the app:
File: '/data/user/0/io.flutter.plugins.imagepicker.example/cache/image_picker3104450369160425156.jpg'
I am using the image_picker package to get the image. This is how:
List<XFile>? _imageFileList;
final ImagePicker _picker = ImagePicker();
void _setImageFileListFromFile(XFile? value) {
_imageFileList = value == null ? null : <XFile>[value];
}
Future<void> _onImageButtonPressed(ImageSource source, {BuildContext? context}) async {
try {
final List<XFile> pickedFileList = await _picker.pickMultiImage();
setState(() {
_imageFileList = pickedFileList;
});
} catch (e) {
log('error');
}
}

Unhandled Exception: HiveError: You need to initialize Hive or provide a path to store the box

I am trying to use Hive database in my flutter app. I have followed the youtube tutorials but the code is not working in my app.
I am getting the following error.
Unhandled Exception: HiveError: You need to initialize Hive or provide a path to store the box.
Here is my code...
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
final dbDir = await getApplicationDocumentsDirectory();
await Hive.initFlutter(dbDir.path);
Hive.registerAdapter(AddToCartAdapter());
await Hive.openBox<AddToCart>('addToCart');
runApp(const MyApp());
}
Future cartlist(String name, String unit, String qty, String price) async {
await Hive.openBox("addToCart");
final cart = AddToCart()
..name = name
..unit = unit
..qty = qty
..price = price;
final Box = Boxes.getAddToCart();
Box.add(cart)
;
}
I also use Hive as a local database. I noticed that I din't specify subDir param and just initiated like that:
await Hive.initFlutter();
Hive.registerAdapter(AddToCartAdapter());
The problem is occurring to you, because you are using Hive.initFlutter with dbDir.path path and while using Hive.openBox you are not specifying that path again, You need to specify same path for openBox method.
I suggest your code should be like that:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
Hive.registerAdapter(AddToCartAdapter());
HiveBoxes.cartBox = await Hive.openBox<AddToCart>(Constants.cartBoxName);
runApp(const MyApp());
}
To make your code better, I suggest some stuffs like creating a class for constant names to prevent typos and creating some static variable to get access Hive Boxes easily like that:
class Constants{
static const cartBoxName = 'CartBox';
}
class HiveBoxes{
static late Box cartBox;
}
About your method you can do like that:
Future cartlist(String name, String unit, String qty, String price) async {
///we don't need to open box again, so please delete this commented code
//await Hive.openBox("addToCart");
final cart = AddToCart()
..name = name
..unit = unit
..qty = qty
..price = price;
HiveBoxes.cartBox.put("sampleKey", cart);;
// It's also wrong code: Box.add(cart);
}

Does Flutter write and read files can be edited on File Explorer or through ADB shell

I didn't know how does File permission works on Flutter write and read mechanism, whether the data can be altered on File Explorer or being edited through ADB shell if the device is rooted.
So, the question is can we access and edit files which has been created using File class outside the Flutter apps if the we knows the exact path where it has been stored? In the example below, let assume the file is file.txt.
Source code
static Future<String> get localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
static Future<File> get localFileBlock async {
final path = await localPath;
//path);
return File('$path/file.txt');
}
//read
static Future<String> readContent() async {
try {
final file = await localFileBlock;
// file.writeAsStringSync('');
String contents = await file.readAsString();
return contents;
} catch (e) {
//e);
return null;
}
}
//write
static Future<File> writeContent(String content) async {
final file = await localFileBlock;
var result = readContentCatalog();
//result);
return file.writeAsString('$content');
}
https://docs.flutter.dev/cookbook/persistence/reading-writing-files

FileSystemException: Cannot open file, path = 'assets/busStops.txt' (OS Error: No such file or directory, errno = 2)

I can't seem to load my txt file stored in assets. Whenever I try to access it, I get the following error:
FileSystemException: Cannot open file, path = 'assets/busStops.txt' (OS Error: No such file or directory, errno = 2)
I've added the file to my pubspec file too:
assets:
- assets/BusSD.png
- assets/SubtractBD.png
- assets/VectorDD.png
- assets/busRoutes.txt
- assets/busStops.txt
This is my code for accessing the file
class FileUtilsBusStopInfo {
static Future<String> get getFilePath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
static Future<File> get getFile async {
//final path = await getFilePath;
return File('assets/busStops.txt');
}
static Future<File> saveToFile(String data) async {
final file = await getFile;
return file.writeAsString(data);
}
static Future<String> readFromFile() async {
try {
final file = await getFile;
String fileContents = await file.readAsString();
print(fileContents);
return fileContents;
} catch (e) {
print(e);
print('returning blank');
return "";
}
}
}
The images load fine though. What can I do to resolve this issue? Thank you in advance!
Try to load a file by:
import 'package:flutter/services.dart' show rootBundle;
// Method
final data = rootBundle.load('assets/busStops.txt');
// ...
Assets in Flutter are loading by its services, not from a device's filesystem.

How to fetch data from *.txt file to Iterable<String'> object in Flutter

I would like to fetch the data from .txt file and save it in "Iterable<String'> object.
Code below is getting data from file to String, but I don't know how to go from here. I would be glad for any help.
Future<String> fetchFileData() async
{
String response;
response = await rootBundle.loadString('assets/file.txt');
}
You can split the String you get from the loadString() by new lines, getting a List<String> which is an Iterable:
Future<List<String>> fetchFileData() async {
String response;
response = await rootBundle.loadString('assets/file.txt');
return response.split('\n');
}
Example call:
List<String> list = await fetchFileData();

Categories

Resources