Created
July 25, 2020 05:58
-
-
Save damiancipolat/a283a8c08f7c3d2898b7fbce44b0c9ef to your computer and use it in GitHub Desktop.
Dart examples
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:convert'; | |
void main(){ | |
var nombre = 'Damian'; | |
var empresa = 'My drugs'; | |
int precio = 100; | |
double pi = 3.141592; | |
bool activado = true; | |
print('Hola Damian'+nombre); | |
print('HI $empresa'); | |
if (activado) | |
print('si'); | |
else | |
print('no'); | |
List numeros = [1,2,3,4,5,6,6]; | |
List<String> perros = ['tito','charlie','popo']; | |
List<int> edades = [20,30,40,50,60]; | |
numeros.add(10); | |
perros.add('Kiki'); | |
print(numeros); | |
print(perros); | |
print(edades); | |
List amigos = new List(); | |
amigos.add('Matias'); | |
amigos.add('Nicolas'); | |
print(amigos); | |
//Todo dinamico | |
Map persona = { | |
'nombre':'carlos', | |
'edad':32, | |
'soltero':true | |
}; | |
print(persona); | |
//Definiendo tipos, como es un mapa solo puedo definir dos campos. | |
Map<String,int> cosas= { | |
'carlos': 20, | |
'damian': 32, | |
'pepe': 20 | |
}; | |
cosas.addAll({'felipe':40}); | |
print(cosas); | |
//Llamar una funcion | |
var resu = sumar(10,30); | |
print(resu); | |
//llamar a una funcion q recibe un objeto como parametro. <--falopa | |
var resu2=saludar(nombre:'damian',apellido:'cipolat'); | |
print(resu2); | |
//Llamar a la arrow function. | |
print(saludar2(nombre:'aa',apellido:'eeee')); | |
//Instancia de clase. | |
final ironman = new Heroe(nombre:'Tony Stark',poder:'inteligencia'); | |
print(ironman.poder); | |
print(ironman.nombre); | |
//Uso el tostring | |
print(ironman); | |
final winter = new Enemigo(nombre:'Winter soldier',poder:'fuerza'); | |
print(winter); | |
//JUGANDO CON JSON. | |
final rawJson = '{"nombre":"damian","poder":"transportacion"}'; | |
final parsedJson = json.decode(rawJson); //retorna <dynamic,dynamic> | |
print(parsedJson); | |
final smith = new Agente.fromJson(parsedJson); | |
print(smith); | |
} | |
//Crear una funcion. | |
int sumar(int a, int b){ | |
return a+b; | |
} | |
//Recibe un objeto | |
String saludar({String nombre, String apellido}){ | |
return nombre+' '+apellido; | |
} | |
//Arrow function | |
String saludar2({String nombre, String apellido})=> apellido+' '+nombre; | |
class Heroe { | |
String nombre; | |
String poder; | |
//Agrego defaults | |
Heroe({String nombre = 'Sin nombre', String poder='sin poder'}){ | |
this.nombre = nombre; | |
this.poder = poder; | |
} | |
//Hago esto para poder hacer un print directo del objeto. | |
String toString(){ | |
return '${this.nombre} ${this.poder}'; | |
} | |
} | |
class Enemigo { | |
String nombre; | |
String poder; | |
//Constructor por defecto, evita hacer asinagciones, this.. | |
Enemigo({this.nombre, this.poder}); | |
//Hago esto para poder hacer un print directo del objeto, NO HACE FALTA EL THIS.. | |
String toString() => '$nombre $poder'; | |
} | |
class Agente { | |
String nombre; | |
String poder; | |
//Constructor por defecto, evita hacer asinagciones, this.. | |
Agente(this.nombre, this.poder); | |
//Hago esto para poder hacer un print directo del objeto, NO HACE FALTA EL THIS.. | |
String toString() => '$nombre $poder'; | |
Agente.fromJson(parsedJson){ | |
nombre = parsedJson['nombre']; | |
poder = parsedJson['poder']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment