skip to Main Content
  • if i touch it once then it prints out 1 line 123

  • if i touch it many times then it prints out many line 123

  • So how when I touch it many times then it prints out 1 line 123 or exiting _launchUrl

  • When I touch it many times then I also had to go back to that number of times to get rid of _launchUrl

My code here
Hope to get everyone’s help!

final Uri _url = Uri.parse('https://flutter.dev');

....


Future<void> _launchUrl() async {
    if (!await launchUrl(_url)) {
        throw 'Could not launch $_url';
    }
}

...

InkWell(
    onTap: () {
        _launchUrl;
        print('123');
    }    
)

I tried using the wait function but still not getting the desired result

2

Answers


  1. Chosen as BEST ANSWER

    I fixed it by following way

    Uri uri = Uri.parse('https://flutter.dev');
    Navigator.of(context).push(
      MaterialPageRoute(
        builder: (context) => LoadURL(uri),
      )
    );
    
    // Navigate to here
    
    import 'package:flutter/material.dart';
    import 'package:url_launcher/url_launcher.dart';
    
    class LoadURL extends StatelessWidget {
      late final Uri uri;
      LoadURL(this.uri);
    
      Future<void> _launchUrl(Uri uri) async {
        if (!await launchUrl(uri)) {
          throw 'Could not launch $uri';
        }
      }
      @override
      Widget build(BuildContext context) {
        return Center(
          child: FutureBuilder(
            future: _launchUrl(uri),
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.done) {
                Navigator.pop(context);
              }
              return CircularProgressIndicator();;
            },
          ),
        );
      }
    }
    

  2. Create a variable buttonPressed and set it default to false

    bool buttonPressed = false;
    

    Inside your onTap you can check if the buttonPressed is set to false. If it is set to false you can set it to true and run your _launchUrl function. After you called _launchUrl you can set it back to false to run it again.

    if(buttonPressed == false){
      buttonPressed = true;
      await _launchUrl();
      buttonPressed = false;
    }
    

    Also mark your onTap as async to use the await keyword

    onTap: () async {
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search