I want to save the value data1 which contains iterable value of CallLog.get() to data1.txt file. I want to know that, how to upload the data1.txt file to google drive. The reason to save the value data1 is to upload it to google drive. Here is the code:
class Data {
Future<Iterable<CallLogEntry>> data1 = CallLog.get();
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
// For your reference print the AppDoc directory
print(directory.path);
return directory.path;
}
Future<File> get _localFile async {
final path = await _localPath;
return File('$path/data1.txt');
}
Future<File> writeContent() async {
final file = await _localFile;
// Write the file
return file.writeAsString('$data1');
}
Future<String> readcontent() async {
try {
final file = await _localFile;
// Read the file
String contents = await file.readAsString();
return contents;
} catch (e) {
// If there is an error reading, return a default String
return 'Error';
}
}
And after upload this, how can I access the data1.txt and show the call logs from data1.txt to a ListView. I have created a Listview to show this but it shows me an error. Here is the error:
Class 'String' has no instance method 'toList'. Receiver: "Error" Tried calling: toList()
Here is the code of my ListView:
FutureBuilder(
future: Data().readcontent(),
builder: (context, snapshot) {
if (!snapshot.hasData)
return Center(child: CircularProgressIndicator());
List<CallLogEntry> entries = snapshot.data.toList();
return Scrollbar(
child: ListView.builder(
itemBuilder: (context, index) {
var entry = entries[index];
var mono = TextStyle(fontFamily: 'monospace');
return Column(
children: <Widget>[
Divider(),
Text('NUMBER : ${entry.number}', style: mono),
Text('NAME : ${entry.name}', style: mono),
Text('TYPE : ${entry.callType}', style: mono),
Text(
'DATE : ${DateTime.fromMillisecondsSinceEpoch(entry.timestamp)}',
style: mono),
Text('DURATION : ${entry.duration}',
style: mono),
],
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
);
},
itemCount: entries.length,
),
);
})