Futures in Dart / flutter_sound examples not working - flutter

Start tinkering with Dart/Flutter, I'm trying to record and play audio. Examples provided in this library: https://github.com/dooboolab/flutter_sound show async code in Dart using Futures.
Future<String> result = await flutterSound.startRecorder(null);
result.then(path) {
print('startRecorder: $path');
_recorderSubscription = flutterSound.onRecorderStateChanged.listen((e) {
DateTime date = new DateTime.fromMillisecondsSinceEpoch(e.currentPosition.toInt());
String txt = DateFormat('mm:ss:SS', 'en_US').format(date);
});
}
However this code doesn't even compile at my system so Im wondering what I'm missing. In order to compile this code I have to change it to something like:
Future<String> result = widget._flutterSound.startRecorder(null);
result.then((path) {
print('startRecorder: $path');
var _recorderSubscription = widget._flutterSound.onRecorderStateChanged.listen((e) {
DateTime date = new DateTime.fromMillisecondsSinceEpoch(e.currentPosition.toInt());
print(date);
});
});
What am I missing?

Have you try this:
Future<String> result() async => flutterSound.startRecorder(null);
when you working with futures: async and await: https://dart.dev/codelabs/async-await
The async and await keywords provide a declarative way to define asynchronous functions and use their results. Remember these two basic guidelines when using async and await:
To define an async function, add async before the function body.
The await keyword works only in async functions.

Related

Flutter function returning at await statement

I am using flutter with the cbl package to persist data. Trying to retrieve the entries does not seem to work because the function created is returning at the await statement and not the return statement. This does not seem like the intended result of darts async/await functionality. So I am lost.
task_database.dart
Future<dynamic> getAllTasks() async {
final tasksDb = await Database.openAsync(database); <---------- Returns here
var tasksQuery = const QueryBuilder()
.select(SelectResult.all())
.from(DataSource.database(tasksDb));
final resultSet = await tasksQuery.execute();
late var task;
await for (final result in resultSet.asStream()) {
final map = result.toPlainMap();
final taskDao = TaskDao.fromJson(map);
task = taskDao.task;
// Do something with the task...
print(task);
}
;
return task; <-------------------------------------------- Does not make it here
}
task_cubit.dart
getAllTasks() => {
allTaskMap = TasksAbcDatabase().getAllTasks(),
emit(TaskState(tasks: state. Tasks))
};
What I have tried. I have tried to use Database.openSync instead of Database.openAsync however, the function just returns at the next await statement. I have also tried making getAllTasks asynchronous and awaiting the database as such.
Future<void> getAllTasks() async => {
allTaskMap = await TasksAbcDatabase().getAllTasks(),
emit(TaskState(tasks: state. Tasks))
};
However this has the same issue, when the function from task_database returns prematurely it the returns at the first await function in getAllTasks which is the allTaskMap variable.
Thanks
A function cannot "return prematurely" without a return statement.
The only way the execution is cut short would be an exception being thrown.
I also don't see how you don't get syntax errors, when you don't await the Database.openAsync(database) statement.
So make sure all your awaits are in place. Use the linter to find those that are missing. While you are at it, remove the keyword dynamic from your vocabulary, it will only hurt you if you use it without a need for it. Your return type should be properly typed, then your compiler could tell you, that returning a single task from a function that is clearly supposed to return multiple tasks is not going to work.
Either catch your exceptions and make sure you know there was one, or do not catch them and watch them go all the way through into your debugger.
In addition, following the comment of #jamesdlin, your function definitions are... valid, but probably not doing what you think they are doing.
Future<void> getAllTasks() async => {
allTaskMap = await TasksAbcDatabase().getAllTasks(),
emit(TaskState(tasks: state. Tasks))
};
needs to be
Future<void> getAllTasks() async {
allTaskMap = await TasksAbcDatabase().getAllTasks();
emit(TaskState(tasks: state. Tasks));
}

Dart Map.map() using async

I have a Dart Map() which I want to perform a .map() operation on to turn it into a second map. However, this conversion requires an await call. How might I do this in Dart? The below gives me an error of course, but how does one achieve this in one line?
Map secondMap = firstMap.map((key, value) async => MapEntry("key", await someAsyncFunction()))
Here is one way to do it:
final secondMap = Map.fromEntries(await Future.wait(firstMap.entries.map((entry) async => MapEntry(entry.key, await someAsyncFunction()))));

Async .sort() in dart

I am working with dart/flutter, and i need to sort large values ​​(10,000, 20,000 90,000 100,000 items ) and the application crashes for a few seconds due to the volume of very large data being processed. my doubt is: Is there any way to use the sort() function as async, to show the loadingProgressIndicator() when the data is sorte?
like this:
listFilt.sort((a,b)=> a.compareTo(b))
And i want to tranform in something like:
showCircularIndicator = true;
await listFilt.sort((a,b)=> a.compareTo(b));
showCircularIndicator = false;
I know that sort() is synchronous function but have any way to transorm in asynchronous function?
Use compute, here is the example of sorting photos according to title.
List<Photo> sortPhotos(List<Photo> photos) {
photos.sort((a,b) => a.title.compareTo(b.title));
return photos;
}
...
final List<Photo> photos = await ...
final result = await compute(sortPhotos, photos);

contact_services getContactsForPhone returing Future<dynamic> instead of String - Flutter

I am trying to get Contact using the function getContactsForPhone
getName()async{
number = '123-456-7890'
return await ContactsService.getContactsForPhone(number).then((value) => value.elementAt(0).displayName.toString());
}
but I am getting Future<dynmaic> instead of .displayName which is supposed to be String
You are mixing it up two way's to use Futures:
You can use await keyword to await for the conclusion.
You can use the then method to have a callback when the Future ends.
You need to choose one and stick with it. Using the await is always preferable because it makes the code more readable and avoids some callback hells.
In your case:
Future<String> getName() async {
number = '123-456-7890'
Iterable<Contact> myIterable = await ContactsService.getContactsForPhone(number);
List<Contact> myList = myIterable.toList();
return myList[0].displayName.toString()
}
Which should return the DisplayName you wanted.
Remember to also use the await keyword from the outside, wherever you call this function.
You can read more about Future and Asynchronous code here.

Flutter Firebase Storage Futures issue

I'm having some problems with a line in the function below. The function is handling async gets from Firebase Storage. I'm using it to get the names and urls of files I have stored there.
The issues is with getting the Urls. Specifically on the line:
String url = element.getDownloadURL().toString();
getDownloadedURL() is a Firebase future. I tried to await it, but it won't recognise the await, I guess due to "element".
The over all effect is that when I'm using this in my UI via a Future builder, the name comes out fine but the Url doesn't. It is being retrieved as the print statement shows it. But it's not being waited for, so the UI is already updated.
Been trying lots of things, but haven't found a solution, so any help would be greatly appreciated.
Future<void> getImageData() async {
final imagesFromStorage = await fb
.storage()
.refFromURL('gs://little-big-deals.appspot.com')
.child('images')
.listAll();
imagesFromStorage.items.forEach((element) {
print(element.name);
String url = element.getDownloadURL().toString();
print(url.toString());
imageData.add(ImageData(element.name, url.toString()));
});
}
Many thanks
You can't use async in forEach.
Just use a for loop:
Future<void> getImageData() async {
final imagesFromStorage = await fb
.storage()
.refFromURL('gs://little-big-deals.appspot.com')
.child('images')
.listAll();
for (var element in imagesFromStorage.items) {
print(element.name);
String url = (await element.getDownloadURL()).toString();
print(url.toString());
imageData.add(ImageData(element.name, url.toString()));
}
}