This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
// Funzione che simula il recupero di dati da un database dopo 2 secondi | |
Future<String> fetchData() async { | |
await Future.delayed(Duration(seconds: 2)); // Simula ritardo | |
return "Dati ricevuti dal database!"; | |
} | |
// Funzione che simula un errore | |
Future<String> fetchWithError() async { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Funzione che simula una divisione, ma lancia un'eccezione se il divisore è zero | |
double dividi(double a, double b) { | |
if (b == 0) { | |
throw ArgumentError("Errore: impossibile dividere per zero!"); | |
} | |
return a / b; | |
} | |
// Funzione che gestisce l'errore ma lo rilancia se è un problema grave | |
void calcola() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
// Classe astratta che definisce una figura geometrica | |
abstract class Figura { | |
double get area; // Getter astratto per l'area | |
double get perimetro; // Getter astratto per il perimetro | |
void descrizione() { | |
print("Sono una figura geometrica."); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
List<int> numbers = [1, 2, 3, 4, 5]; | |
// forEach: Itera sulla lista ed esegue un'operazione su ogni elemento | |
print("forEach:"); | |
numbers.forEach((num) => print(num * 2)); | |
// map: Trasforma ogni elemento della lista e restituisce una nuova lista | |
List<int> doubledNumbers = numbers.map((num) => num * 2).toList(); | |
print("\nmap: $doubledNumbers"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Funzione senza parametri | |
void saluta() { | |
print("Ciao, mondo!"); | |
} | |
// Funzione con parametri posizionali | |
void somma(int a, int b) { | |
print("Somma: ${a + b}"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// main: entry point dell'applicazione | |
void main() { | |
// variabile | |
var j = 0; | |
// costante: non può mai vambiare valore | |
final k = 5; | |
// costante il cui valore è noto a compile time | |
const x = 10; |