Dart / Flutter - Validating a string for URL

Jake picture Jake · Oct 24, 2018 · Viewed 18.7k times · Source

I'm trying to create a system in which I can validate a string to check if it's a url or not. (https://www.google.com/)

I found the flutter package validator but this isn't dart 2 compatible, so won't be compatible with my code.

Similarly named, I also found the validators package but I can't seem to get it working correctly with my code, as it throws the following error; (hope you like my project name ;)

Because every version of flutter_test from sdk depends on test 1.3.0 
and every version of validators depends on test ^1.3.3, flutter_test from sdk is incompatible with validators.
So, because gucci depends on both validators ^1.0.0+1 and flutter_test any from sdk, version solving failed.
Unable to reload your application because "flutter packages get" failed to update package dependencies.
Exception: pub get failed (1)

If you could either find a fix for validators so that it doesn't throw this error and thus works correctly with my code, or suggest another method of validating a string to be URL compliant that would be great.

Thanks

Edit - My pubspec.yaml file

name: gucci
description: A new Flutter project.

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons:
  barcode_scan:
  gradient_app_bar:
  url_launcher:
  validate: ^1.7.0

dev_dependencies:
  flutter_test:
    sdk: flutter

  fonts:
    - family: PlayfairDisplay
      fonts:
        - asset: fonts/PlayfairDisplay-BoldItalic.ttf

    - family: Kanit
      fonts:
        - asset: fonts/Kanit-ExtraBoldItalic.ttf

    - family: Poppins
      fonts:
        - asset: fonts/Poppins-BoldItalic.ttf

    - family: PoppinsLightItalic
      fonts:
        - asset: fonts/Poppins-LightItalic.ttf

    - family: PoppinsMediumItalic
      fonts:
        - asset: fonts/Poppins-MediumItalic.ttf

Answer

iPatel picture iPatel · Oct 4, 2019

To check Valid URL string you just have to use Uri.parse() like below.

bool _validURL = Uri.parse(_adVertData.webLink).isAbsolute;

Just check value of _validURL