Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Goto function in Dart?

Tags:

flutter

dart

Is there a function in Dart which acts as an equivalent to the Goto function, wherein I could transfer the programs control to a specified label.

For example:

var prefs = await SharedPreferences.getInstance();
if (prefs.getString("TimetableCache") == null || refreshing) {
    var response = await http.get(
    Uri.encodeFull("A website",);
    data = JsonDecoder().convert(response.body);
    try {
        if (response != null) {
            prefs.setString("TimetableCache", response.body);
        }
    } catch (Exception) {
        debugPrint(Exception);
    }
   } else {
data = prefs.getString("TimetableCache");
}

if (data != null) {
    try {
       //Cool stuff happens here
    } catch (Exception) {
    prefs.setString("TimetableCache", null);
    }
}

I have an http request and before I want to move on with my 'cool stuff' I have a try catch which sees if anything is in the TimetableCache location of the SharedPreferences of the machine. When it catches the exception I would ideal have a goto method to send it back to the top line again to retry getting the data.

In c# you can use goto refresh;, for example, and the code will then start executing wherever the identifier refresh: is.

Is there a dart version of this?

like image 573
R. Duggan Avatar asked Jan 11 '19 19:01

R. Duggan


People also ask

What is the function of goto?

GoTo (goto, GOTO, GO TO or other case combinations, depending on the programming language) is a statement found in many computer programming languages. It performs a one-way transfer of control to another line of code; in contrast a function call normally returns control.

How do you skip a loop in darts?

The continue statement skips the subsequent statements in the current iteration and takes the control back to the beginning of the loop. Unlike the break statement, the continue statement doesn't exit the loop.

How do you use goto?

The goto statement can be used to alter the flow of control in a program. Although the goto statement can be used to create loops with finite repetition times, use of other loop structures such as for, while, and do while is recommended. The use of the goto statement requires a label to be defined in the program.

Can goto be used to jump across functions?

You can't.


1 Answers

Yes, Dart supports labels. With continue and break you can jump to labels.

https://www.tutorialspoint.com/dart_programming/dart_programming_loops.htm

void main() { 
   outerloop: // This is the label name 

   for (var i = 0; i < 5; i++) { 
      print("Innerloop: ${i}"); 
      innerloop: 

      for (var j = 0; j < 5; j++) { 
         if (j > 3 ) break ; 

         // Quit the innermost loop 
         if (i == 2) break innerloop; 

         // Do the same thing 
         if (i == 4) break outerloop; 

         // Quit the outer loop 
         print("Innerloop: ${j}"); 
      } 
   } 
}

void main() { 
   outerloop: // This is the label name 

   for (var i = 0; i < 3; i++) { 
      print("Outerloop:${i}"); 

      for (var j = 0; j < 5; j++) { 
         if (j == 3){ 
            continue outerloop; 
         } 
         print("Innerloop:${j}"); 
      } 
   } 
}

https://github.com/dart-lang/sdk/issues/30011

switch (x) {
  case 0:
    ...
    continue foo; // s_c
  foo:
  case 1: // s_E (does not enclose s_c)
    ...
    break;
}

See also

  • https://stackoverflow.com/a/12641818/217408
  • Labels - break vs continue vs goto
like image 183
Günter Zöchbauer Avatar answered Oct 16 '22 22:10

Günter Zöchbauer