79771235

Date: 2025-09-22 04:47:07
Score: 2
Natty:
Report link

I'm new to Flutter, so here's a solution that worked in my project; it may not be the only way. I just wanted to provide my own version for new users like me.

Notes:

import 'package:web/web.dart' as web;

// link to open
const answerUrl = 'https://stackoverflow.com/questions/ask';

// function to open link in new tab or same page
void openUrl(String url, {bool newTab = true}) {
  try {
    if (newTab) {
      // opens new tab
      final newWindow = web.window.open(url, '_blank');
      if (newWindow == null) {
        // Fallback if browser blocks the popup
        web.window.location.href = url;
      }
    } else {
      // Open directly in the same tab
      web.window.location.href = url;
    }
  } catch (_) {
    // Fallback for cases like Google App on iPhone
    web.window.location.href = url;
  }
}

// ... passing the link to the function inside onPressed
ElevatedButton(
  onPressed: () => openUrl(answerUrl),
  child: const Text("Go to questions"),
)
Reasons:
  • Blacklisted phrase (1): stackoverflow
  • RegEx Blacklisted phrase (1.5): I'm new
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Shamnas Codes