Files
app_notificaciones_ador/lib/BloC/contenedores_event.dart
2024-05-16 16:13:59 +02:00

54 lines
1.7 KiB
Dart

import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:http/http.dart' as http;
import 'package:html/parser.dart' as htmlParser;
import 'package:html/dom.dart' as htmlDom;
import 'package:flutter_project/pages/embalses';
// Define el estado
class MyState {
}
// Define el bloc
class MyBloc extends Cubit<MyState> {
MyBloc() : super(MyState());
}
//mejorar código
Future<List<String>> obtenerDatos(String url) async {
final response = await http.get(Uri.parse('http://www.crcivan.com/escaparate/noticias.cgi?idnoticias=192683'));
if (response.statusCode == 200) {
final document = htmlParser.parse(response.body);
final elementosSpan = document.querySelectorAll('html > body > div > center > table > tbody > tr > td > div > center > table > tbody > tr > td > table > tbody > tr > td > font > font > table > tbody > tr > td > span');
List<String> embalsesData = [];
for (var elemento in elementosSpan) {
final style = elemento.attributes['style'];
if (style != null && style.contains('color: #000000; font-size: 12pt;')) {
embalsesData.add(elemento.text);
}
}
return embalsesData;
} else {
throw Exception('Error al cargar los datos de embalses');
}
}
Future<void> launchAemetURL() async {
const urlString = 'https://www.aemet.es/es/eltiempo/prediccion/municipios/caspe-id50074';
final url = Uri.parse(urlString);
try {
await launch(urlString);
/* if (await canLaunch(urlString)) {
await launch(urlString);
} else {
throw 'Could not launch $urlString';
}*/
} catch (e) {
throw Exception('Error al lanzar la URL de Aemet: $e');
}
}