Alert Dialog running infinitely - flutter

Hello I am trying to run following code, I want to run a specific asynchronous code and show alert dialog until it's running. But the code is not being executed after await showAlertDialog(); this line.
void appendAndRunPythonCode() async {
await showAlertDialog();
await runPythonScript(final_code);
_alertDialogUtils.dismissAlertDialog(context);
}
This is how my showAlertDialog() function is implemented:
Future<void> showAlertDialog() async {
if (!_alertDialogUtils.isShowing) {
await _alertDialogUtils.showAlertDialog(context);
}
}
runPythonCode():
Future<void> runPythonScript(String code) async {
if (inputImg == null) {
ToastUtils.showToastMessage(text: ConstUtils.input_image_empty_notice);
return;
}
if (code.isEmpty) {
ToastUtils.showToastMessage(text: ConstUtils.code_empty);
return;
}
List<String> lines = code.split('\n');
String lastLine = lines.elementAt(lines.length - 4);
if (lastLine.split(' ').elementAt(0).compareTo('outputImage') != 0) {
ToastUtils.showToastMessage(text: ConstUtils.cv_error_line2);
return;
}
data.putIfAbsent("code", () => code);
data.putIfAbsent("inputImg", () => inputImg);
_alertDialogUtils.showAlertDialog(context);
final result = await _channel.invokeMethod("runPythonCVScript", data);
// Add Artifical Delay of 3 seconds..
await Future.delayed(
Duration(seconds: 3),
);
_alertDialogUtils.dismissAlertDialog(context);
setState(
() {
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
curve: Curves.easeOut,
duration: const Duration(milliseconds: 300),
);
output = result['textOutput'] ??= "";
error = result['error'] ??= "";
outputImg = (result['graphOutput']);
data.clear();
},
);
}

You shouldn't await the showAlertDialog because runPythonScript won't be executed until the dialog is dismissed.
Remove the await.
Like so:
void appendAndRunPythonCode() async {
showAlertDialog();
await runPythonScript(final_code);
_alertDialogUtils.dismissAlertDialog(context);
}

Related

Clarifying asynchronous operations in dart

In the async codelab:
Ref: https://dart.dev/codelabs/async-await
They have the following snippet:
Future<void> printOrderMessage() async {
print('Awaiting user order...');
var order = await fetchUserOrder();
print('Your order is: $order');
}
Future<String> fetchUserOrder() {
// Imagine that this function is more complex and slow.
return Future.delayed(const Duration(seconds: 4), () => 'Large Latte');
}
void main() async {
countSeconds(4);
await printOrderMessage();
}
Based on the logic of printOrderMessage() I've made a similar function:
void main() async {
int? value;
value = await test();
print(value);
}
Future<int?> test() async{
print('Function has started');
Future.delayed(Duration(milliseconds: 2000), () {
return 4;
});
}
In my case, it prints Function has started null. why doesn't it wait for the value to be populated
Future<int?> test() async {
print('Function has started');
await Future.delayed(Duration(milliseconds: 2000), () {});
return 4;
}
Future<int?> test() async {
print('Function has started');
return Future.delayed(Duration(milliseconds: 2000), () {
return 4;
});
}

`Stream.toList` stuck forever in a fake_async environment

void main() {
test('simple', () async {
await fakeAsync((async) async {
final stream = Stream.value(42);
print('hi before await');
await stream.toList();
print('hi after await');
});
});
}
stucks forever in the toList line...
I have also tried to call whatever methods, but still stuck forever in the "await" line
test('simple', () async {
await fakeAsync((async) async {
final stream = Stream.value(42);
final future = stream.toList();
print('hi before await');
async.flushMicrotasks();
async.flushTimers();
async.elapse(const Duration(seconds: 10));
async.elapseBlocking(const Duration(seconds: 10));
await future;
print('hi after await');
});
});
I have looked at the implementation of Stream.toList as follows, but it seems quite normal
Future<List<T>> toList() {
List<T> result = <T>[];
_Future<List<T>> future = new _Future<List<T>>();
this.listen(
(T data) {
result.add(data);
},
onError: future._completeError,
onDone: () {
future._complete(result);
},
cancelOnError: true);
return future;
}

stream work fine for the first time but then stops

Future<bool> connectServer(BuildContext context) async {
try {
// await _displayTextInputDialog(context);
// socket = await Socket.connect(_textFieldController.text, 2508,
// timeout: const Duration(seconds: 4));
socket = await Socket.connect("192.168.14.148", 2508,
timeout: const Duration(seconds: 4));
stream = socket.listen(null);
return true;
} on SocketException {
await Navigator.pushNamed(
context,
'/connectionE',
);
return false;
}
}
Future<dynamic> receiveMessage() {
final completer = Completer();
print("before");
stream.onData((data) {
print(data);
if (data.contains(49)) {
print(data);
completer.complete(extractData(data));
stream.cancel();
}
});
print('after');
return completer.future;
}
Future<String> sendAndWait(
BuildContext context, String message, int code) async {
try {
sendMessage(message, code);
print("sended");
String reply = await receiveMessage();
stream.onData((data) {});
print(reply);
return "";
} on SocketException {
Navigator.pushReplacementNamed(
context,
'/serverDownE',
);
}
return "2";
}
receiveMessage work on the first time, but then stop working, it doesn't even get into the onData function. Does someone know why is this happening? I cant use cancel on the subscription, cancel and start new one because when I start listening again I get an error Bad state stream has already been listening. I can't close the socket because the the server is a stateful server.

Dart - returning a result from an async function

I was experimenting with asynchronous programming in dart when I stumbled upon a problem in which when I put a return statement inside a Future.delayed function it doesn't seem to return a value.
void main() {
perform();
}
void perform() async {
String result = await firstTask();
finalTask(result);
}
Future firstTask() async {
Duration duration = Duration(seconds: 4);
String result = 'task 2 data';
await Future.delayed(duration, () {
print('First Task Completed');
return result;
});
}
void finalTask(String result) {
print('final task completed and returned $result');
}
but if I put the return result; statement outside the Future.delayed function it returns its value to task 3. like,
void main() {
perform();
}
void perform() async {
String result = await firstTask();
finalTask(result);
}
Future firstTask() async {
Duration duration = Duration(seconds: 4);
String result = 'task 2 data';
await Future.delayed(duration, () {
print('First Task Completed');
});
return result;
}
void finalTask(String result) {
print('final task completed and returned $result');
}
Your first task doesn't have any return statement. IDE should be warning you about it. To fix it you have to do
Future firstTask() async {
Duration duration = Duration(seconds: 4);
String result = 'task 2 data';
return await Future.delayed(duration, () {
print('First Task Completed');
return result;
});
}
Or
Future firstTask() { // No async here
Duration duration = Duration(seconds: 4);
String result = 'task 2 data';
return Future.delayed(duration, () {
print('First Task Completed');
return result;
});
}

type 'future<dynamic>' is not a subtype of type 'function'

when i run my app in debug mode it shows me the error "type 'future' is not a subtype of type 'function'" all over the screen and also in the debug console. Can someone help me? I imagine it's a problem with async functions "reset","rateoGet" and "rateoSave" but i can't find any solution.
P.S. I've deleted part of the code because it was useless for this question.
int plus;
int min;
int per;
int div;
double val;
int gameswon =0;
int moves;
static int mosse=15;
String win = "gioca";
int games=0;
double rateo=1;
String mode;
int flag;
var timer=30;
#override
void initState() {
super.initState();
reset();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body:
MyButton(text: "$per" ,color: Colors.deepPurpleAccent, onTap: (){
setState(() {
val*=per;
});
if(widget.mode=="timermode" && flag==0){
timerceckresults();
}else if(widget.mode=="movesmode"){
checkResult();
}
},
MyBottomButton(text: "Reset",color: Colors.indigo,width:160, onTap: reset()),
),
}
checkResult() {
if(val == 101) {
print("hai vinto");
win="Hai Vinto";
setState(() {});
gameswon++;
Timer(Duration(seconds: 2), () {
reset();
});
} else {
print("ci sei quasi");
moves++;
mosse--;
win="$mosse moves left";
setState(() {});
if(moves>14){
print("hai perso coglione");
win="Hai Perso Coglione";
setState(() {});
Timer(Duration(seconds: 2), () {
reset();
});
}
}
}
timerceckresults(){
flag=1;
timer = 30;
Timer.periodic(Duration(seconds: 1), (t){
timer--;
setState(() {
win = "${timer.toString()}seconds left";
});
if(val==101){
timer=0;
}
if(timer == 0) {
t.cancel();
if(val == 101) {
win="Hai Vinto";
setState(() {});
gameswon++;
Timer(Duration(seconds: 2), () {
reset();
});
} else {
win="Hai Perso Coglione";
setState(() {});
Timer(Duration(seconds: 2), () {
reset();
});
}
}
});
static int randNum(x,y) {
var rng = new Random();
return rng.nextInt(y-x)+x;
}
reset() async{
timer=1;
plus = randNum(4, 9);
min = randNum(5, 19);
per = randNum(3, 9);
div = randNum(2, 5);
val = randNum(2, 11).toDouble();
moves = 0;
mosse=15;
if(widget.mode=="timermode"){
win="start playing";
}else{
win="$mosse moves left";
}
await rateoSave();
await rateoGet();
games++;
rateo=gameswon/(games-1);
await rateoSave();
flag=0;
setState(() {});
}
rateoSave() async {
SharedPreferences prefs=await SharedPreferences.getInstance();
await prefs.setInt("games",games);
await prefs.setInt("gameswon",gameswon);
}
rateoGet() async {
SharedPreferences prefs=await SharedPreferences.getInstance();
games=(prefs.getInt("games") ?? 0);
gameswon=(prefs.getInt("gameswon") ?? 0);
https://dart.dev/codelabs/async-await read this before you check the answer will help you alot
reset() async{
timer=1;
plus = randNum(4, 9);
min = randNum(5, 19);
per = randNum(3, 9);
div = randNum(2, 5);
val = randNum(2, 11).toDouble();
moves = 0;
mosse=15;
if(widget.mode=="timermode"){
win="start playing";
}else{
win="$mosse moves left";
}
await rateoSave();
await rateoGet();
games++;
rateo=gameswon/(games-1);
await rateoSave();
flag=0;
setState(() {});
}
Future<bool> rateoSave() {
SharedPreferences prefs= SharedPreferences.getInstance();
prefs.setInt("games",games);
prefs.setInt("gameswon",gameswon);
return true;
}
Future<bool> rateoGet() async {
SharedPreferences prefs= SharedPreferences.getInstance();
await games=(prefs.getInt("games") ?? 0);
await gameswon=(prefs.getInt("gameswon") ?? 0);
return true;
}
you are trying to get a variable from a method that returns a future. you need to add await just before you make the call to that function.
can you tell us in which line this error occurs ?
The most important thing to keep in mind is that if anything in your call-chain returns a Future, everything above it must deal with futures, either by returning the future itself (if no processing must be done), or await'ing and dealing with the returned value (but you'll still be returning a future).