Diferencia entre revisiones de «Flutter DART Programación asíncrona»

De MediaWiki
Ir a la navegación Ir a la búsqueda
Línea 16: Línea 16:
  
  
−
:<syntaxhighlight lang="java" enclose="div" highlight="" >
+
 
 +
 
 +
:<syntaxhighlight lang="java" enclose="div" highlight="5,13" >
 
void main() {
 
void main() {
  
 
   print('Inicia programa');
 
   print('Inicia programa');
 
    
 
    
−
   try{
+
   getImagenes('imagenes/animales').then(
−
  getImages('imagenes/animales').then((datos) => print(datos),onError: (e) => print('error:$e'));
+
    (datos) => print(datos)
−
  }
+
   );
−
   catch(e){
 
−
    print('Error con throw');
 
−
  }
 
 
    
 
    
 
   print('Fin programa');
 
   print('Fin programa');
Línea 32: Línea 31:
 
}
 
}
  
−
Future getImages(String path){
+
Future<List<String>> getImagenes(String path){
 +
 
 +
  List<String> datos = ['1.jpg','2.jpg','3.jpg','4.jpg'];
 
    
 
    
−
  List<String> imagenes = ['1.jpg','2.jpg','3.jpg'];
 
 
    
 
    
−
   // Buscar imágenes en base al path dado
+
   return Future.delayed(Duration(seconds: 4),() => datos); // Paramos el hilo durante 4 segundos
−
  //return Future.delayed(Duration(seconds: 2),() => throw('Error en la busqueda'));
 
−
  throw('Error en la busqueda');
 
−
  //return Future.error('Error durante la búsqueda');
 
 
    
 
    
−
  return Future.value(imagenes);
 
 
    
 
    
 +
  // return Future.value(datos);
 
    
 
    
 
}
 
}
Línea 49: Línea 46:
  
  
 +
<br />
 +
===Gestión de errores===
 +
void main() {
 +
 +
  print('Inicia programa');
 +
 
 +
  try {
 +
    getImages('imagenes/animales').then((datos) {
 +
      datos.forEach((elem) => print('Dato:$elem'));
 +
    }).catchError((e) { print(e);} );
 +
   
 +
  }
 +
  catch(e){
 +
    print('Excep:$e');
 +
  }
 +
  print('Fin programa');
 +
 
 +
}
 +
 +
Future<List<String>> getImages(String path){
 +
 
 +
  List<String> imagenes = ['1.jpg','2.jpg','3.jpg'];
 +
 
 +
  // Búsqueda de imágenes a partir del path
 +
 
 +
  //throw('Exepción no controlada');
 +
 
 +
  return Future.delayed(Duration(seconds: 3),() => Future.error('Error controlado'));
 +
 
 +
 
 +
 
 +
}
  
  

Revisión del 10:08 12 jul 2021

Introducción

  • La programación asíncrona hace referencia al uso de threads para ejecutar en modo 'paralelo' al hilo principal otra serie de tareas.



Futures

  • Más información:



void main() {

  print('Inicia programa');
  
  getImagenes('imagenes/animales').then(
    (datos) => print(datos)
  );
  
  print('Fin programa');
  
}

Future<List<String>> getImagenes(String path){
  
  List<String> datos = ['1.jpg','2.jpg','3.jpg','4.jpg'];
  
  
  return Future.delayed(Duration(seconds: 4),() => datos);  // Paramos el hilo durante 4 segundos
  
  
  // return Future.value(datos);
  
}



Gestión de errores

void main() {

 print('Inicia programa');
 
 try {
   getImages('imagenes/animales').then((datos) {
     datos.forEach((elem) => print('Dato:$elem'));
    }).catchError((e) { print(e);} );
   
 }
 catch(e){
   print('Excep:$e');
 }
 print('Fin programa');
 

}

Future<List<String>> getImages(String path){

 List<String> imagenes = ['1.jpg','2.jpg','3.jpg'];
 
 // Búsqueda de imágenes a partir del path
 
 //throw('Exepción no controlada');
 
 return Future.delayed(Duration(seconds: 3),() => Future.error('Error controlado'));
 
 
 

}



Enlace a la página principal de la UD2

Enlace a la página principal del curso




-- Ángel D. Fernández González -- (2021).