I want to return a String from an async function but I get a Future
What am I doing wrong;
Example
main() {
String s;
s = dummy("http://www.google.com");
}
String dummy(String s) {
String response;
response = readURL(s);
return response;
}
Future<String> readURL(String requestString) async {
String response = await http.read(requestString);
print(response);
return response;
}
Error:
type '_Future' is not a subtype of type 'String' of 'response'.
A function that's annotated with async will always return a Future.
so when you call readUrl(s) you can await its result.
To use await, the caller (here your main function) has to be marked as async. So the end result could look like this:
main() async {
String s = await dummy("http://www.google.com");
}
Future<String> dummy(String s) async {
String response = await readURL(s);
return (response);
}
Future<String> readURL(String requestString) async {
String response = await http.read(requestString);
print(response);
return(response);
}
The thing to notice here: If you use await in a function, it is now considered as function that returns a Future. So every function you convert to be async will now return a Future.
Here is the Simple Two way to get value from Function with return type Future<Type>
1- First way (best way, as you call this code from any file)
FutureFunctionName.then((val) {
val contains data
});
For example- (I am posting one from real example)
Future<String> getUserAgents() async {
String userAgent;
await FlutterUserAgent.init();
userAgent = FlutterUserAgent.webViewUserAgent;
return userAgent;
}
String userAgent;
getUserAgents().then((val) {
userAgent = val;
});
print(userAgent); // you will get output
2- Second way (use a global variable to get data)
String userAgent;
Future<void> getUserAgents() async {
await FlutterUserAgent.init();
userAgent = FlutterUserAgent.webViewUserAgent;
}
print(userAgent);
Related
Making a network request is easy to Python and what makes it easy is that sync request.
In Dart, I can make a request like this:
HttpClient client = new HttpClient();
client.getUrl(Uri.parse("http://www.example.com/"))
.then((HttpClientRequest request) {
// Optionally set up headers...
// Optionally write to the request object...
// Then call close.
...
return request.close();
})
.then((HttpClientResponse response) {
// Process the response.
...
});
Obviously it's async request. In my opinion, above code can be reused many times. So I want to make a request and return a JSON object.
getResponse(String url) async {
HttpClient httpClient = new HttpClient();
HttpClientRequest request = await httpClient.getUrl(Uri.parse(url));
HttpClientResponse response = await request.close();
String responseBody = await response.transform(utf8.decoder).join();
Map jsonResponse = jsonDecode(responseBody) as Map;
httpClient.close();
return jsonResponse;
}
As you see, the above method getResponse returns Future<dynamic>. So how can I call it and get the json value?
To get the dynamic from inside the Future you do one of the following:
// option 1 async method
MyAsyncMethod() async {
dynamic result = await getResponse("http://google.com");
if (result is Map) {
// process the data
}
}
// option 2 callback with .then()
MyNonAsyncMethod() {
getResponse("http://google.com").then ( (dynamic result) {
if (result is Map) {
// process the data
}
});
}
Note that your own async method can also return a Future<something> and be treated in the same two ways when called.
Where map is a nested Map<String, Dynamic> and result is an object of type of your creation that conforms to Json serialization interface (See this link).
To access Data in the map:
//given example json structure
var map = {
"myPropertyName":50,
"myArrayProperty":[
"anArrayEntry"
],
"mySubobject": {
"subObjectProperty":"someValue"
}
};
var myProperty = map["myPropertyName"]; // get a property value from the object
var myArrayEntry = map["myArrayProperty"][0]; // get first element of an array property
var mySubobjectPropertyValue = map["mySubobject"]["subObjectProperty"]; // get a property value from a subobject
i have a function called foo which is listening to the stdout, what i want is to return some string which i got from stdout. here is my function;
dynamic foo(process) {
return (
process.stdout.transform(UTF8.decoder).listen((data) {
String s = data.toString();
// print(s);
if (s.contains("received event of")) {
var s1 = s.split(":");
print("${s1[1]}");
return s1[1];
}
}));
}
I want to return s1 to the calling function
here a callback function do the trick
foo(process, callback) {
process.stdout.transform(UTF8.decoder).listen((data) {
String s = data.toString();
if (s.contains("received event of")) {
String message = s.split(":")[1];
callback(message);
}
});
}
and here i am calling the method and printing the data which i get get from stream.
foo(process,(data){print(data);})
This should do what you want
Future<String> dynamic foo(process) {
return process.stdout.transform(UTF8.decoder).map((data) {
String s = data.toString();
// print(s);
if (s.contains("received event of")) {
var s1 = s.split(":");
print("${s1[1]}");
return s1[1];
} else {
return null;
}
}).where((val) => val != null).first;
}
Your custom code either returns a valid value or null.
I changed listen to map to be able to use additional stream methods.
where filters invalid values (null) and returns the first non-null value.
The caller of the foo method needs to handle the returned Future (using for example async/await) to get the value when it becomes available.
Use it like
bar() async {
...
var input = await foo(proc);
print(input);
}
I think that everybody wants this:
import 'dart:io';
import 'dart:convert';
// Using system encoding:
var outputStr = await process.stdout.transform(systemEncoding.decoder).join();
// Using UTF-8 encoding:
var outputStr = await process.stdout.transform(utf8.decoder).join();
I am trying to understand the usage of async and await in Dart. Somehow I am having issues returning values in certain methods.
Consider the code below
Future<int> getMrn() async {
var mrnRef = await firebaseClient.child('mrn');
DataSnapshot ss;
StreamSubscription<Event> onValueSubscription = await mrnRef.onValue
.listen((event) {
ss = event.snapshot;
return ss.val();
});
//return Future<int> ss.val();
}
mrn is of type int which should be returned by getMrn method. However each time the returned ss.val() returns null. It seems that ss = event.snapshot is not seen in the last returned value
What is the correct way of doing this.
Thanks
In the code above, you're declaring anonymous function (event){..} as a callback, and your return statement relates to it, while your intention was to return from getMrn().
What are you actually need, is to complete a Future you're returning from getMrn() inside your callback.
Like this:
Future<int> getMrn() async {
var mrnRef = await firebaseClient.child('mrn');
Completer<int> c = new Completer<int>();
StreamSubscription<Event> onValueSubscription = await mrnRef.onValue
.listen((event) {
DataSnapshot ss = event.snapshot;
c.complete(ss.val());
});
return c.future;
}
but that code wouldn't work good if there second event appear in mrnRef.onValue stream. So, assuming mrnRef.onValue is a Stream, and you need only first event, it would be better to rewrite it this way:
Future<int> getMrn() async {
var mrnRef = await firebaseClient.child('mrn');
Event event = await mrnRef.onValue.first;
DataSnapshot ss = event.snapshot;
// note, you're implicitly returning a Future<int> here,
// because our function is asyncronous
return ss.val();
}
Hey have I read all I can find about futures, but I would like some more advice on proper usage.
I am writing an API library, that bridges the gap between HTTP Requests and the app. So I use the future returned by HTTP in most cases, however sometimes the data is already retrieved. Is that the appropriate time to use a Completer?
ex.
String _someData = "";
Future<String> getSomeData(){
if (_someData == ""){
return Api.getSomeData().then((String someData){
_someData = someData;
return _someData;
});
} else {
var completer = new Completer();
completer.complete(_someData);
return completer.future;
}
}
-edit- Also if I create a Completer, but end up not using its future or calling complete. Will that cause a mem leak? Should I call its complete method or dispose of it somehow?
Thanks :)
You can use the named constructor Future.value if the value is immediately accessible. You won't need a Completer.
String _someData = "";
Future<String> getSomeData(){
if (_someData == ""){
return Api.getSomeData().then((String someData){
_someData = someData;
return _someData;
});
} else {
return new Future.value(_someData);
}
}
And for your second question, if you create a Completer without using it, I guess the garbage collector will simply free its memory when there won't be anymore references to it in your code.
Use an async function instead.
import "dart:async";
String _someData = "";
Future<String> getSomeData() async {
if (_someData == "") {
_someData = await Api.getSomeData();
}
return _someData;
}
Compiler generates approximately the following code:
import "dart:async";
String _someData = "";
Future<String> getSomeData() {
var $_awaiter = new Completer<String>();
try {
if (_someData == "") {
Api.getSomeData().then(($) {
$_awaiter.complete($);
}).catchError((e, s) {
$_awaiter.completeError(e, s);
});
return $_awaiter.future;
} else {
$_awaiter.complete(_someData);
return $_awaiter.future;
}
} catch (e, s) {
$_awaiter.completeError(e, s);
return $_awaiter.future;
}
}
I am trying to call a web service which is supposed to run an async task. This async task should iterate through an array of records and process them. Each record to be processed generates a result. What I want is to concatenate in a list these results. When the web service is called, I want to retrieve such list from the HttpResponse of the caller but I do not have an idea how to do it.
The code of the caller function is:
private void ProcessRecords(List<Record> recordList)
{
//The WS is called and a response is awaited
HttpResponseMessage response = client.PostAsJsonAsync("/api/mycontroller/myws/", recordList).Result;
//TODO: Read the result list from the http response
}
The code of my Web Service is as follows:
[HttpPost]
[Route("myws")]
public async Task<IHttpActionResult> WebServiceMethod()
{
var jsonString = await Request.Content.ReadAsStringAsync();
List<Record> recordList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Record>>(jsonString);
Task<Result> resultTask = CreateTaskToProcessRecords(recordList);
//TODO I Do not now what to return here in order
//for it to contain the resultTask variable and later await it in the user function
}
private Task<List<Result>> CreateTaskToProcessRecords(List<Record> recordList)
{
var newTask = Task.Run<List<Result>>(() =>
{
List<Result> resultList = new List<Result>();
try
{
foreach(Record record in recordList)
{
var result = DoSomething(record);
resultList.Add(result);
}
return resultList;
}
catch (Exception ex)
{
return resultList;
}
});
return newTask;
}
What I am trying to do is to somehow return a Task> to the function that calls the web service so that the whole processing done in the web service "newTask" remains asynchronous.
Do you have any ideas?
Thanks
Luis.
All your work is synchronous. There's no need for async or await here, so don't use them. (And as a general rule, avoid Task.Run on ASP.NET).
[HttpPost]
[Route("myws")]
public IHttpActionResult WebServiceMethod()
{
var jsonString = await Request.Content.ReadAsStringAsync();
List<Record> recordList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Record>>(jsonString);
var results = ProcessRecords(recordList);
return Json(results);
}
private List<Result> ProcessRecords(List<Record> recordList)
{
List<Result> resultList = new List<Result>();
try
{
foreach(Record record in recordList)
{
var result = DoSomething(record);
resultList.Add(result);
}
return resultList;
}
catch (Exception ex)
{
return resultList;
}
}
Note that you can still consume it asynchronously on the client:
private async Task ProcessRecordsAsync(List<Record> recordList)
{
// The WS is called and a response is awaited
HttpResponseMessage response = await client.PostAsJsonAsync("/api/mycontroller/myws/", recordList);
var result = await response.Content.ReadAsAsync<List<Result>>();
}