Last active
January 22, 2020 14:26
-
-
Save palcodes/54aea7d6ac84182b7e5eeca563648f69 to your computer and use it in GitHub Desktop.
Function to use substring & calculate ageRange
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
calculateAgeRange() async { | |
FirebaseAuth.instance.currentUser().then((user) async { | |
final snapShot = | |
await Firestore.instance.collection('User').document(user.uid).get(); | |
User userSnapshot = User.fromSnapshot(snapShot); | |
if (userSnapshot.AgeRange == '') { | |
if (userSnapshot.DateOfBirth == '') { | |
print('DATE OF BIRTH IS NULL'); | |
} else { | |
String ageRange; | |
int picked = int.parse(userSnapshot.DateOfBirth.substring(6)); | |
DateTime currentDate = DateTime.now(); | |
var age = currentDate.year - picked; | |
if (age > 18 && age <= 25) { | |
print('age range is 18 - 25'); | |
ageRange = '18 - 25 years'; | |
} else if (age > 25 && age <= 29) { | |
print('age range is 20 - 29'); | |
ageRange = '20 - 29 years'; | |
} else if (age > 29 && age <= 35) { | |
print('age range is 30 - 35'); | |
ageRange = '30 - 35 years'; | |
} else if (age > 35) { | |
print('age range is 35 and above'); | |
ageRange = '35 & above'; | |
} | |
print('AGERANGE: ' + ageRange); | |
profileBloc.ageRangeSink(ageRange); | |
} | |
} else if (snapShot.data.containsKey('AgeRange') == false) { | |
print('AGE NOT FOUND'); | |
return null; | |
} else { | |
print('AGE RANGE: ' + userSnapshot.AgeRange); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works !