I do not think you can sort by name. I believe you can only sort by “newest” or “oldest”.
I may not understand your question, so please ask again if this does not help. However, I am using the listFiles to get the last image taken.
The code below reliably gives me the last file taken.
import 'dart:convert';
import 'package:http/http.dart' as http;
Future<String> getLastImageUrl() async {
var url ='http://192.168.1.1/osc/commands/execute';
Map data = {
'name': 'camera.listFiles',
'parameters': {
'fileType': 'image',
'entryCount': 1,
'maxThumbSize': 0
}
};
//encode Map to JSON
var body = jsonEncode(data);
var response = await http.post(url,
headers: {"Content-Type": "application/json;charset=utf-8"},
body: body
);
Map<String, dynamic> thetaImage = jsonDecode(response.body);
// test data
// final testFileUrl = 'https://picsum.photos/200/300/?random';
String imageFileUrl = thetaImage['results']['entries'][0]['fileUrl'];
return imageFileUrl;
}
Waiting for Process to Finish
If you are taking the picture, then listing it, you may need to wait for the takePicture process to return a ready status.
Example of checking camera status
Note that the example below is using the status command to check to make sure the camera is ready for the next command.
https://api.ricoh/docs/theta-web-api-v2.1/protocols/commands_status/
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:imagetest/pretty_print.dart';
import 'package:imagetest/take_picture.dart';
Future<String> isDone(String id) async {
var url ='http://192.168.1.1/osc/commands/status';
Map data = {
'id': id
};
var payload = jsonEncode(data);
var response = await http.post(url,
headers: {"Content-Type": "application/json;charset=utf-8"},
body: payload
);
Map<String, dynamic> status = jsonDecode(response.body);
String state = status['state'];
return state;
}
Future<String> downloadReady () async {
print("Test of taking picture and then checking to see if picture is ready for download");
print("---");
var takePictureResponse = await takePicture();
// print(takePictureResponse);
Map<String, dynamic> pictureInfo = jsonDecode(takePictureResponse.body);
String id = pictureInfo['id'];
print('The status ID is $id');
bool keepGoing = true;
int elapsedSeconds = 0;
while (keepGoing) {
var currentStatus = await isDone(id);
// print(currentStatus);
await new Future.delayed(const Duration(seconds: 1));
print("Elapsed time: $elapsedSeconds seconds. State: $currentStatus");
elapsedSeconds++;
if (currentStatus == "done") {
keepGoing = false;
}
}
print("picture ready for download");
return 'ready';
}
Output
I/flutter (20524): Test of taking picture and then checking to see if picture is ready for download
I/flutter (20524): ---
I/flutter (20524): The HTTP response code is: 200
I/flutter (20524): The HTTP response from camera.takePicture is:
I/flutter (20524): {
I/flutter (20524): "name": "camera.takePicture",
I/flutter (20524): "id": "269",
I/flutter (20524): "progress": {
I/flutter (20524): "completion": 0.0
I/flutter (20524): },
I/flutter (20524): "state": "inProgress"
I/flutter (20524): }
I/flutter (20524): The status ID is 269
I/flutter (20524): Elapsed time: 0 seconds. State: inProgress
I/flutter (20524): Elapsed time: 1 seconds. State: inProgress
I/flutter (20524): Elapsed time: 2 seconds. State: inProgress
I/flutter (20524): Elapsed time: 3 seconds. State: inProgress
I/flutter (20524): Elapsed time: 4 seconds. State: inProgress
I/flutter (20524): Elapsed time: 5 seconds. State: inProgress
I/flutter (20524): Elapsed time: 6 seconds. State: inProgress
I/flutter (20524): Elapsed time: 7 seconds. State: done
I/flutter (20524): picture ready for download
I/flutter (20524): Image is available at thi URL
I/flutter (20524): http://192.168.1.1/files/thetasc26c21a247d9055838792badc5/100RICOH/R0010049.JPG