Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart / Flutter - Validating a string for URL

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
like image 943
Jake Avatar asked Oct 24 '18 18:10

Jake


People also ask

How to validate textformfiled or textfield data with Dart regex in flutter?

isUUID - check if the string is a UUID (version 3, 4 or 5). matches - check if string str matches the Regex pattern. In this way, you can validate TextFormFiled or TextField data with Dart Regex and package in Flutter App.

How to add validators flutter packag E?

For that, add validators flutter packag e to your dependency by adding the following line in pubspec.yaml file: contains - check if the string contains the seed. equals - check if the string matches the comparison. isAfter - check if the string is a date that's after the specified date.

What is string validation and sanitization for Dart?

String validation and sanitization for Dart. The is a new fork of validator.dart, which was originally a port of validator.js. equals (String str, comparison) - check if the string matches the comparison.

How to validate data without using regex in flutter?

if(fname.text.length > 10){ //length of textfield with controller "name" has more than 10 characters. } Furthermore, you can validate data easily without using Regex. For that, add validators flutter packag e to your dependency by adding the following line in pubspec.yaml file: contains - check if the string contains the seed.


5 Answers

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

like image 117
iPatel Avatar answered Oct 17 '22 19:10

iPatel


Uri.tryParse(mystring)?.hasAbsolutePath ?? false;

Some example results:

url result
'https://stackoverflow.com/questions/52975739/dart-flutter-validating-a-string-for-url' true
asd false
asd:asd false
%EMPTY_STRING% false
google.nl false
https: false
https:// false
https://a false
https://a/ true
like image 21
Robin Dijkhof Avatar answered Oct 17 '22 17:10

Robin Dijkhof


var urlPattern = r"(https?|http)://([-A-Z0-9.]+)(/[-A-Z0-9+&@#/%=~_|!:,.;]*)?(\?[A-Z0-9+&@#/%=~_|!:‌​,.;]*)?";
var match = new RegExp(urlPattern, caseSensitive: false).firstMatch('https://www.google.com');

You can use RegExp too.

like image 12
John Wang Avatar answered Oct 17 '22 19:10

John Wang


I used the following method below. Depending on your rec, all valid URLs need to have a host (ex, google.com). If a URL does not have a host it returns an empty string (not undefined or null).

  bool isURLValid = Uri.parse('https://google.com/').host.isNotEmpty;

Using .isAbsolute, as some have already reported, marks URLs such as 'http:' to be valid URLs which not.

like image 9
Sethu Senthil Avatar answered Oct 17 '22 19:10

Sethu Senthil


For some reason, the validators package is requiring a pretty recent version of flutter's testing library rather than leaving it up to the application. Maybe there's a good reason for that (i.e. they're using a new feature).

The flutter engine internally requires a particular version of the flutter_test library (which is why it's generally a bad idea to specify a version of it). So to fix this you'll have to upgrade your flutter by running flutter upgrade. If you're already at the most recent version of the channel you're in, you may have to run flutter channel dev or flutter channel master to switch to a channel (branch really) that's updated more often.

I run on the dev branch/channel for the most part and while it very occasionally has problems, it doesn't happen a lot. I'd advise against using the master branch if possible though.

like image 6
rmtmckenzie Avatar answered Oct 17 '22 19:10

rmtmckenzie