Flutter | How to check if String does not contain a substring or a character
Asked Answered
C

1

5

I'm learning Flutter/Dart and there is this check that i want to perform - to validate that a user who has entered an email should contain @ in it

String myString = 'Dart';

// just like i can do the following check
myString.contains('a') ? print('validate') : print('does not validate')

// I want to do another check here
myString does not contain('@') ? print('does not validate'): print('validate')

Can someone suggest is there any inbuilt function to do such a thing.

Churr answered 15/9, 2021 at 15:6 Comment(0)
G
9

Just simply put not ! operator (also known as bang operator on null-safety) on start

void main() {
  String myString = 'Dart';

// just like i can do the following check
  myString.contains('a') ? print('validate') : print('does not validate');

// I want to do another check here
  !myString.contains('@') ? print('does not validate') : print('validate');
}

Gynaecology answered 15/9, 2021 at 15:8 Comment(3)
I'm not aware of any inbuilt function for this.Gynaecology
Okk! So i can use this bang operator pretty much with everythingChurr
dart bool check = false; !check ? print('getting the hang of it') : print('not yet'); Churr

© 2022 - 2024 — McMap. All rights reserved.