I currently have a widget that I use to open a specific webpage using the LaunchUrl feature from the URL_Launcher package.
Throughout my app there are several instances where I'd like to open a webpage using this exact method, but I suppose it's not very efficient to copy-paste the widget I created for this over and over again, and simply replacing the URL.
This is the widget I have written, which is working. But this one is opening a specific URL.
Future<void> openHCReservationPortal(String url) async {
final url = Uri.parse('https://portal.mijnhandicart.nl/site/login');
if (!await launchUrl(url, mode: LaunchMode.platformDefault)) {
throw Exception('Kon $url niet openen');
}
}
I'd like to have the url be a parameter, so I can reuse the widget.
I have tried to create a statefull widget with a required parameter but got all sorts of errors, so I must be missing something or doing something wrong.
This is what I wrote:
class OpenWebPageWidget extends StatefulWidget {
final String url;
const OpenWebPageWidget({super.key, required this.url});
@override
State<OpenWebPageWidget> createState() => _OpenWebPageWidgetState();
}
class _OpenWebPageWidgetState extends State<OpenWebPageWidget> {
@override
Widget build(BuildContext context) {
Future<void> openWebPageWidget(String url) async {
final url = Uri.parse(url);
if (!await launchUrl(url, mode: LaunchMode.platformDefault)) {
throw Exception('Kon $url niet openen');
}
}
}
}
Flutter is complaining that:
- The body might complete normally, causing 'null' to be returned, but the return type, 'Widget', is a potentially non-nullable type. Try adding either a return or a throw statement at the end.
- The declaration 'openWebPageWidget' isn't referenced.