Function expressions can't be named: then( - flutter

Im having issues with my code and since I'm new at using Flutter, I have no clue on how to fix it. I was trying to fix the http.get(string) and I kind of did, but now I'm having issues with then(()).
void submitForm(FeedbackForm feedbackForm) async {
try {
await http.get(Uri.parse(URL + feedbackForm.toParams()).then((response)) {
callback(convert.jsonDecode(response.body['status']));
});
} catch (e) {
print(e);
}
}
}

It seems you got a parenthesis missplaced:
await http.get(...).then((response) => callback(...))
The them allows you to use the result of the previous Future, as soon as it becomes available. If you find it confusing you can declare one variable at a time.
final response = await http.get(...);
// Check if response was as expected
await callback();

Related

async/await method throws exception -type int is not a subtype of bool

I am calling a graphql endpoint which returns success, but I do get an exception on the calling method.
Here is my calling method -
await AmplifyInstance()// this is where I get the exception. Snip below
.createUserOnAzureCosmosDB(user)
.then((result) {
print(result['data']['userPhoneNumber']);
_intlPhoneFieldController.text =
(result['data']['userPhoneNumber'].toString())
.substring(1);
_incrementStep('continueOnProfilePictureWidget');
});
Here is the called method -
Future<dynamic> createUserOnAzureCosmosDB(User user) async {
HttpLink link = GlobalVariables().graphqlEndpoint;
GraphQLClient graphQlClient = GraphQLClient(
link: link,
cache: GraphQLCache(
store: InMemoryStore(),
),
);
try {
QueryResult mutationResult = await graphQlClient.mutate(
//Mutation query here
if (mutationResult.data?['createUser'] != null) {
print('Created user on Cosmos DB');
registerUserStatus['result'] = true;
registerUserStatus['data'] = mutationResult.data?['createUser'];
}
} on ApiException catch (e) {
print('Mutation failed: $e');
registerUserStatus['result'] = false;
registerUserStatus['errorMessage'] = e.message;
}
return registerUserStatus;
}
And the returned registerUserStatus is just an array -
var registerUserStatus = {};
Here is the exception -
UPDATE eamirho3ein
Here is the result of print("result=$result);
I/flutter (14224): result = {result: true, data: {__typename: User, partitionKey: user, userPhoneNumber: 14160000000, userDisplayName: testuser, avatarUrl: www.url.com, createdAt: Today}}
This is not actually an answer, but rather a way to find the answer more easily yourself:
then chains make it increasingly hard to find your problem, because the compiler/debugger/IDE has a harder time pointing you to it. So don't do it.
With async/await available from the beginning, there never has been a reason to use then in any Dart program.
await AmplifyInstance().createUserOnAzureCosmosDB(user).then((result) {
Is equivalent to just writing:
final result = await AmplifyInstance().createUserOnAzureCosmosDB(user);
And then continuing on with the code you had put in the lambda function in the then part. Obviously, you need to remove the closing bracket somewhere too now.
This way, your error will actually pop up where it happens, not at the await of a huge chain that leaves you wondering what the problem might be.

Run multiple asyn function flutter one after one flutter

hello I want have to run two functions(Function1() and Function2()) and store value of these returns and run third function. But some time according to condition Function1() or Function2() or both not be run.
if(condition1){
await Function1();
}
if(condition2){
await Function2();
}
await Functon3();
I try as above but Function3() run simultaneously with Function1() or with Function2().
My Function1() code looks like following...
Future Function1() async {
apiService
.apiFileUpload()
.then((value) async {
///codes goes here
}).catchError((error) {
print('EEEE: ' + error.toString());
});
}
If anything not clear please let me know in the comment section.
Please do not use .then() in combination with async/await. It is technically possible, but it takes some skill to get it right, so why make it hard on yourself. Stick with one way of doing it, use either one or the other. You mixed it up and through a slight oversight, your Function1 does not actually wait on it's result. It just returns, with the function still running in the then block. So you await it, but that does not help.
Since you are using await already, stick with that and remove .then() from your repertoire for now:
Future Function1() async {
try {
final value = await apiService.apiFileUpload();
///codes goes here
} catch(error) {
print('EEEE: ' + error.toString());
}
}
You can use await
Future Function1() async {
try{
final value = await apiService
.apiFileUpload();
final value2 = await secondFuntion();
///add more and condition on values
} catch(e){
.....
}
}
from your question you need to tell the compiler to stop on particular task with await and avoid using then function it will never stop your compiler
your future fuction:
Future Function1() async {
apiService
.apiFileUpload()
.then((value) async {
///codes goes here
}).catchError((error) {
print('EEEE: ' + error.toString());
});
}
Modified Future func
Future Function1() async {
var result = await apiService.apiFileUpload();
if(result == success){
// code goes here
}else{
//you can show your error here
}
}

Dart Future wait to complete before calling another Future

Looking for help here guys. I have 2 Futures that populate data in sqflite. I need one (_insertInitialData()) to finish first before calling the second one (_insertAdditionalData()). I have done it this way, but it's not working. It first does the re-recreation of the DB as expected, then both _insertInitialData() and _insertAdditionalData() not in the order I expected. I have tried .whenComplete and calling here _insertAdditionalData() and also have tried different ways that I think should work but nothing.
This is just something I'm doing for fun but still I'd like to know what I'm doing wrong.
TIA
Future<void> _insertAdditionalData() async{}
void populateDB() {
try {
final localDB = LocalDatabase.instance;
Future.wait([localDB.dropDB(recreateDB: true)]).then((_){
print('DB recreated!');
Future.wait([_insertInitialData()]).then((_) {
print('_insertInitialData done');
Future.wait([_insertAdditionalData()]).then((_){
print('_insertAdditionalData done');
});
});
});
} catch (ex) {
print('There was a problem in populateDB(): $ex');
}
}```
Using await makes things a lot easier to read and will block execution of later lines until the action has finished.
Future<void> populateDB() async {
final localDB = LocalDatabase.instance;
await localDB.dropDB(recreateDB: true);
print('DB recreated!');
await _insertInitialData();
print('_insertInitialData done');
await _insertAdditionalData();
print('_insertAdditionalData done');
}
You can try using only one future statement, and follows it with multiple "then". This makes sure the first "then" will be completed before the second "then" is executed. I assume your localDB.dropDB(recreateDB: true) function is asynchronous.
Future<void> _insertAdditionalData() async{}
void populateDB() {
try {
final localDB = LocalDatabase.instance;
localDB.dropDB(recreateDB: true).then((_){
print('DB recreated!');
}).then((_) {
_insertInitialData();
print('_insertInitialData done');
}).then(() {
_insertAdditionalData();
print('_insertAdditionalData done');
});
} catch (ex) {
print('There was a problem in populateDB(): $ex');
}
}
Cool. Thanks for your comments. I replaced all of those Future.wait for async/await even in my internal methods that insert into the DB and it's all good!!! (executing in the sequence I need)
Just learning Dart/Flutter for fun and I love it.

How to conditionally click an element in Protractor?

In a protractor test, I need to close a pop-up if it appears (it doesn't always) and proceed with the test as normal. Here's the code I've got to do so-
let checkForPopUp = async function() {
element(by.css('button[id="gdprStopEmails"]')).isPresent().then(function (isVisible) {
return isVisible;
});
}
it('description', async function() {
let hasPopUp = await checkForPopUp();
if(hasPopUp) {
await element(by.id("gdprStopEmails")).click();
}
await connectedAccounts.revokePermission(partnerInfo.revokeId, partnerInfo.confirmRevokeId);
});
I ran this test a few times without checking if the element was there, and it closed the popup every time it was there (and failed the test when it wasn't). It hasn't closed the popup a single time since I introduced the condition check, and despite my best efforts I can't figure out what's up. Does anything jump out to you guys? Thanks in advance!
you're missing return. Your function returns nothing explicitly and thus implicitly returns undefined which is always falsy, and your if block doesn't get executed
This should work
let checkForPopUp = async function() {
return element(by.css('button[id="gdprStopEmails"]')).isPresent().then(function (isVisible) {
return isVisible;
});
}
it('description', async function() {
let hasPopUp = await checkForPopUp();
if(hasPopUp) {
await element(by.id("gdprStopEmails")).click();
}
await connectedAccounts.revokePermission(partnerInfo.revokeId, partnerInfo.confirmRevokeId);
});
but since you're using async/await don't mix syntaxes. I'd do it this way
let checkForPopUp = async function() {
return element(by.css('button[id="gdprStopEmails"]')).isPresent()
}
it('description', async function() {
let hasPopUp = await checkForPopUp();
if(hasPopUp) {
await element(by.id("gdprStopEmails")).click();
}
await connectedAccounts.revokePermission(partnerInfo.revokeId, partnerInfo.confirmRevokeId);
});
and keep in mind isPresent doesn't guarantee the visibility

How to check if an element exists or not in flutter driver(QA environment)?

I would like to check if an element exists or not. Something like a function that returns a Boolean. Or something similar to a function in Selenium 'ifExists' which wouldn't throw an exception if the element didn't necessarily exist and it would go about continuing the process without stopping in between when an element isn't found. There are similar things that exist on flutter_test but I have been unable to use it alongside flutter_driver so far.
According to Flutter issue #15852, there is currently no such possibility so far.
But one workaround mentioned in this issue by the user jonsamwell is to use the waitFor method by flutter driver and wrap it in a try/catch to wait if it times out. If it times out, the element is not there, if it does not time out, the element is present:
Future<void> testStep() async {
final isOpen = await isPresent(find.byType("Drawer"), world.driver);
if (isOpen) {
...
}
}
Future<bool> isPresent(SerializableFinder finder, FlutterDriver driver, {Duration timeout = const Duration(seconds: 1)}) async {
try {
await driver.waitFor(finder, timeout: timeout);
return true;
} catch (e) {
return false;
}
}
Obviously, you have to calculate the waiting time according to your use-case to consider any loading times.
try-catch approach didn't work for me.
I have written a peace of code to do the trick for me
Future<bool> isPresent(SerializableFinder finder, FlutterDriver driver, {Duration timeout = const Duration(seconds: 1)}) async {
Stopwatch s = new Stopwatch();
s.start();
await driver.waitFor(finder, timeout: timeout);
s.stop();
if(s.elapsedMilliseconds >= timeout.inMilliseconds){
return false;
}else{
return true;
}
}
Future<void> testStep() async {
final exists = await isPresent(find.byValueKey("accountMenu"), driver);
if (exists ) {
...
}
}
if the time it took for the "driver.waitFor" function to find our widget was more than the timeout, then the widget is not present.