├── 01-main.dart ├── 02-tipos-numero-string.dart ├── 03-bool-condicion.dart ├── 04-listas.dart ├── 05-map.dart ├── 06-funciones.dart ├── 07-clase-basica.dart ├── 08-named-constructor.dart ├── 09-getters-setters.dart ├── 10-clases-abstractas.dart ├── 11-extends.dart ├── 12-mixins.dart ├── 13-futures.dart ├── 14-async-await.dart └── README.md /01-main.dart: -------------------------------------------------------------------------------- 1 | 2 | 3 | void main() { 4 | 5 | // Este es un comentario 6 | 7 | /* 8 | * Esto es un comentario multilinea 9 | * Hola de nuevo... 10 | */ 11 | 12 | 13 | print('Hola Mundo'); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /02-tipos-numero-string.dart: -------------------------------------------------------------------------------- 1 | 2 | void main() { 3 | 4 | // Strings 5 | final String nombre = 'Tony'; 6 | final apellido = 'Stark'; 7 | 8 | // nombre = 'Peter'; 9 | 10 | print('$nombre $apellido'); 11 | 12 | 13 | // Números 14 | int empleados = 10; 15 | double salario = 1856.25; 16 | 17 | print( empleados ); 18 | print( salario ); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /03-bool-condicion.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | 3 | 4 | bool? isActive = null; 5 | 6 | if ( isActive == null ) { 7 | print( 'isActive es null' ); 8 | } else { 9 | print( 'No es null' ); 10 | } 11 | 12 | 13 | } 14 | -------------------------------------------------------------------------------- /04-listas.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | 3 | 4 | List numeros = [1,2,3,4,5,6,7,8,9,10]; 5 | numeros.add(11); 6 | print( numeros ); 7 | 8 | final masNumeros = List.generate(100, (int index) => index ); 9 | 10 | print(masNumeros); 11 | } 12 | -------------------------------------------------------------------------------- /05-map.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | 3 | // Map persona = { 4 | // 'nombre': 'Fernando', 5 | // 'edad': 35, 6 | // 'soltero': false, 7 | // true: false, 8 | // 1: 100, 9 | // 2: 500 10 | // }; 11 | 12 | Map persona = { 13 | 'nombre': 'Fernando', 14 | 'edad': 35, 15 | 'soltero': false, 16 | }; 17 | 18 | persona.addAll({ 'segundoNombre': 'Juan' }); 19 | 20 | print( persona ); 21 | } 22 | -------------------------------------------------------------------------------- /06-funciones.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | 3 | final nombre = 'Fernando'; 4 | 5 | // saludar( nombre, 'Greetings' ); 6 | saludar2( nombre: nombre, mensaje: 'Greetings' ); 7 | 8 | } 9 | 10 | void saludar( String nombre, [ String mensaje = 'Hi' ]) { 11 | print('$mensaje $nombre'); 12 | } 13 | 14 | 15 | void saludar2({ 16 | required String nombre, 17 | required String mensaje, 18 | }) { 19 | print('$mensaje $nombre'); 20 | } 21 | 22 | 23 | -------------------------------------------------------------------------------- /07-clase-basica.dart: -------------------------------------------------------------------------------- 1 | void main(){ 2 | 3 | final wolverine = new Heroe(nombre:'Logan', poder: 'Regeneración'); 4 | 5 | // wolverine.nombre = 'Logan'; 6 | // wolverine.poder = 'Regeneración'; 7 | 8 | print( wolverine ); 9 | 10 | } 11 | 12 | 13 | class Heroe { 14 | 15 | String nombre; 16 | String poder; 17 | 18 | Heroe({ 19 | required this.nombre, 20 | required this.poder 21 | }); 22 | 23 | // Heroe( String pNombre ) { 24 | // this.nombre = pNombre; 25 | // } 26 | 27 | 28 | String toString() { 29 | return 'Heroe: nombre: ${this.nombre}, poder: ${ this.poder }'; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /08-named-constructor.dart: -------------------------------------------------------------------------------- 1 | void main(){ 2 | 3 | final rawJson = { 4 | 'nombre': 'Tony Stark', 5 | 'poder': 'Dinero' 6 | }; 7 | 8 | // final ironman = new Heroe( nombre: rawJson['nombre']!, poder: rawJson['poder']! ); 9 | 10 | final ironman = Heroe.fromJson( rawJson ); 11 | 12 | print(ironman); 13 | 14 | // final wolverine = new Heroe(nombre:'Logan', poder: 'Regeneración'); 15 | // print( wolverine ); 16 | 17 | } 18 | 19 | 20 | class Heroe { 21 | 22 | String nombre; 23 | String poder; 24 | 25 | Heroe({ 26 | required this.nombre, 27 | required this.poder 28 | }); 29 | 30 | Heroe.fromJson( Map json ): 31 | this.nombre = json['nombre']!, 32 | this.poder = json['poder'] ?? 'No tiene poder'; 33 | 34 | 35 | 36 | String toString() { 37 | return 'Heroe: nombre: ${this.nombre}, poder: ${ this.poder }'; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /09-getters-setters.dart: -------------------------------------------------------------------------------- 1 | import 'dart:math' as math; 2 | 3 | void main() { 4 | 5 | final cuadrado = new Cuadrado( 2 ); 6 | 7 | cuadrado.area = 100; 8 | 9 | print( 'area: ${ cuadrado.calculaArea() }' ); 10 | 11 | print( 'lado: ${ cuadrado.lado }' ); 12 | print( 'area get: ${ cuadrado.area }' ); 13 | 14 | 15 | 16 | } 17 | 18 | 19 | class Cuadrado { 20 | 21 | double lado; // lado * lado 22 | 23 | 24 | double get area { 25 | return this.lado * this.lado; 26 | } 27 | 28 | set area( double valor ) { 29 | this.lado = math.sqrt(valor); 30 | } 31 | 32 | 33 | 34 | Cuadrado( double lado ): 35 | this.lado = lado; 36 | 37 | double calculaArea() { 38 | return this.lado * this.lado; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /10-clases-abstractas.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | 3 | final perro = new Perro(); 4 | final gato = new Gato(); 5 | 6 | 7 | sonidoAnimal( perro ); 8 | sonidoAnimal( gato ); 9 | 10 | } 11 | 12 | void sonidoAnimal( Animal animal ) { 13 | animal.emitirSonido(); 14 | } 15 | 16 | 17 | abstract class Animal { 18 | 19 | int? patas; 20 | void emitirSonido(); 21 | 22 | } 23 | 24 | class Perro implements Animal { 25 | 26 | int? patas; 27 | 28 | void emitirSonido() => print('Guauuuuuuuu'); 29 | 30 | } 31 | 32 | class Gato implements Animal { 33 | 34 | int? patas; 35 | int? cola; 36 | 37 | void emitirSonido() => print('Miauuuuuuuu'); 38 | } 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /11-extends.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | 3 | final superman = new Heroe('Clark Kent'); 4 | final luthor = new Villano('Lex Luthor'); 5 | 6 | 7 | print( superman ); 8 | print( luthor ); 9 | } 10 | 11 | 12 | abstract class Personaje { 13 | String? poder; 14 | String nombre; 15 | 16 | Personaje( this.nombre ); 17 | 18 | @override 19 | String toString() { 20 | return '$nombre - $poder'; 21 | } 22 | 23 | } 24 | 25 | 26 | class Heroe extends Personaje { 27 | 28 | int valentia = 100; 29 | 30 | Heroe( String nombre ): super( nombre ); 31 | 32 | } 33 | 34 | class Villano extends Personaje { 35 | 36 | int maldad = 50; 37 | 38 | Villano( String nombre ): super( nombre ); 39 | } 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /12-mixins.dart: -------------------------------------------------------------------------------- 1 | abstract class Animal { } 2 | 3 | abstract class Mamifero extends Animal { } 4 | abstract class Ave extends Animal { } 5 | abstract class Pez extends Animal { } 6 | 7 | abstract class Volador { 8 | void volar() => print('estoy volando'); 9 | } 10 | 11 | abstract class Caminante { 12 | void caminar() => print('estoy caminando'); 13 | } 14 | 15 | abstract class Nadador { 16 | void nadar() => print('estoy nadando'); 17 | } 18 | 19 | class Delfin extends Mamifero with Nadador{} 20 | 21 | class Murcielago extends Mamifero with Caminante, Volador {} 22 | 23 | class Gato extends Mamifero with Caminante {} 24 | 25 | class Paloma extends Ave with Caminante, Volador {} 26 | 27 | class Pato extends Ave with Caminante, Volador, Nadador {} 28 | 29 | class Tiburon extends Pez with Nadador {} 30 | 31 | class PezVolador extends Pez with Nadador, Volador {} 32 | 33 | 34 | void main() { 35 | 36 | 37 | // final flipper = new Delfin(); 38 | // flipper.nadar(); 39 | 40 | // final batman = new Murcielago(); 41 | // batman.caminar(); 42 | // batman.volar(); 43 | 44 | } 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /13-futures.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | 3 | print('Antes de la petición'); 4 | 5 | httpGet('https://api.nasa.com/aliens') 6 | .then( (data) { 7 | 8 | print( data.toUpperCase() ); 9 | 10 | }); 11 | 12 | 13 | print('Fin del programa'); 14 | 15 | } 16 | 17 | 18 | 19 | 20 | Future httpGet( String url ) { 21 | return Future.delayed( 22 | Duration( seconds: 3 ), () =>'Hola Mundo - 3 segundos' 23 | ); 24 | } 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /14-async-await.dart: -------------------------------------------------------------------------------- 1 | void main() async { 2 | 3 | print('Antes de la petición'); 4 | 5 | final data = await httpGet('https://api.nasa.com/aliens'); 6 | 7 | print( data ); 8 | 9 | // final nombre = await getNombre( '10' ); 10 | // print( nombre ); 11 | // getNombre( '10' ).then( print ); 12 | 13 | 14 | print('Fin del programa'); 15 | 16 | } 17 | 18 | 19 | Future getNombre( String id ) async { 20 | return '$id - Fernando'; 21 | } 22 | 23 | 24 | 25 | 26 | 27 | Future httpGet( String url ) { 28 | return Future.delayed( 29 | Duration( seconds: 3 ), () =>'Hola Mundo - 3 segundos' 30 | ); 31 | } 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Dart Basics 2 | Un repositorio con los ejercicios de reforzamiento de Dart para el curso de Flutter 3 | 4 | [Dart de cero hasta los detalles](https://fernando-herrera.com/#/search/dart) --------------------------------------------------------------------------------