firebase security rules: optional field that could be a null or a string? - google-cloud-firestore

How to make firebase accept a key that could be a null or a string ? and yet the field is optional, since in security rules you canot ceck if (is) is null ?
function dataCheckCreate(requestData) {
return (
// requestData.count required
requestData.count is number &&
// requestData.src required
requestData.src is string &&
// requestData.date optional !!
// if available it could be a null or a string
(requestData.date == null || requestData.date is string)
);
}

The last rule will be true when date is either equal to null or is a string. If you want that field to be optional then try:
function dataCheckCreate(requestData) {
return (
requestData.count is number &&
requestData.src is string &&
// [date not present in data keys] [date is string]
(!('date' in requestData.keys()) || requestData.date is string)
);
}

You cant store null in a string field on firebase. The equivalent is to not store it at all.
Javascript will set the key date to undefined/null if it doesn't exist.
Since it's either null or string and optional, do you really need to check it? This would be a suitable check:
function dataCheckCreate(requestData) {
return requestData.count && requestData.src
}

Related

dart null safty issue when try to add value to map element

I am using Dart but i am facing null safty issue with following code
RxMap<Product,int>cartItems=Map<Product,int>().obs;
void updateCart(Product product,String type){
if(type=="plus") {
cartItems.value[product]++;
}
else {
cartItems.value[product]--;
}
}
i got the following error message
the method '+' can't be unconditionally invoked because the receiver can be 'null'
i tried to add null check to the target as following
cartItems.value![product]++;
You can give a default value if null.
cartItems.value[product]??0 +1
Or force assert to non null value like this.It may throw exception if element not present in HashMap
cartItems.value[product]!+1
In your question you are asserting not null for HashMap not the value of the HashMap.
The problem is that cartItems.value is a Map and it's possible that cartItems.value[product] is null. In this case you can't add or remove 1 to null.
So you should do like the following:
if (type == "plus") {
cartItems.value[product] = (cartItems.value[product] ?? 0) + 1;
} else {
cartItems.value[product] = (cartItems.value[product] ?? 0) - 1;
}
Using (cartItems.value[product] ?? 0) you're saying that if cartItems.value[product] is null 0 is used instead.
Also note that in the else clause, when cartItems.value[product] == null, you're trying to remove 1 to something that doesn't exist, so in that case it may be best to throw an exception:
int? currentValue = cartItems.value[product];
if (currentValue == null) {
throw Exception('Trying to remove on a null object');
}
cartItems.value[product] = currentValue - 1;

Check variable run time Type in flutter with conditions like "123" is present as a String but is a int so how can i check this?

I have to check runtime Type of value for this I am using :-
for Example:-
String a = "abc";
int b = 123;
var c = "123"; //this is int value but because of this double quotes is represent as a String
a.runtimeType == String //true
b.runtimeType == int // true
c.runtimeType == String //true
c.runtimeType == int //false
a = "abc" // okay
b = 123 //okay
c = "123" //is issue
now I have to call a api with only String body in this case :-
this c is called the API because is String but i know this is a int value which is present as a String, so I have to stop this.
How can I check this??
when I am using try catch so my program is stopped because of FormatException error.
Note:- I don't know the real value of C, may be its "123" or "65dev" or "strjf" this value is changed every time.
and if i am parsing this in int to this return an error in many case.
Ok i understood that you want to pass "123" by checking and if it is int you are passing it , My question is what you will do if it is "123fe" you are going to pass as string? or you will pass nothing.
I don't know how you're passing it to API but if you wanna pass integer value from string quoted variable, you can parse/convert to integer like this.
int.parse(c);
either you can pass it directly or you can store in another variable and pass that variable.
Alternatively if you've int value and to have to pass it as a string, simply parse like this
integerValue.toString();
according to your code
b.toString();
Edit
String a = '20';
String b = 'a20';
try{
int check = int.parse(a);
//call your api inside try then inside if
if(check.runtimeType == int){
print('parsed $check');
}
}
catch(e){
print('not parsed ');
//handle your error
throw(e);
}
This will definitely help you!
String name = "5Syed8Ibrahim";
final RegExp nameRegExp = RegExp(r'^[a-zA-Z ][a-zA-Z ]*[a-zA-Z ]$');
print(nameRegExp.hasMatch(name));
//output false
name = "syed ibrahim";
print(nameRegExp.hasMatch(name));
//output true
Just check the output and based on that boolean value invoke api call
I hope it will done the work

EF Core - detect actual property value changes

I'm attempting to detect which fields/properties/columns have changed in DbContext's SaveChanges() method. I'm currently using the following code:
string fields = "";
foreach (var entityProperty in ent.Entity.GetType().GetProperties())
{
var propertyName = entityProperty.Name;
var currentValue = ent.Property(propertyName).CurrentValue;
var originalValue = ent.Property(propertyName).OriginalValue;
if ( currentValue == null && originalValue != null ||
originalValue == null && currentValue != null ||
(currentValue != null && originalValue != null &&
!originalValue.Equals(currentValue))
)
{
fields += propertyName + ", ";
}
}
My code frequently sets the values of columns to their current value so I can't rely on PropertyEntry.IsModified - that seems to get set to true even if you set a field to the same value it already has.
Also, in the above code, originalValue is coming out as the NEW value of the column, so this code isn't working either.
Does anyone know how I can detect which fields/properties/columns have actually had their value changed?

Error: A value of type 'num?' can't be assigned to a variable of type 'num' because 'num?

I found this error while running the dart project
Error: A value of type 'num?' can't be assigned to a variable of type 'num' because 'num?'
import 'dart:io';
void main() {
print("Enter your birth-year");
var n = num.tryParse(stdin.readLineSync() ?? "");
if(n=="") {
print("Bad Year");
}
var age = DateTime.now().year-n;
print(" ==> You are $age year old!");
}
As you can read in the documentation of num.tryParse, the method has an return type of num?. This implies that your variable n is also of the type num? (Nullable num).
So the error points you to the line, where you want to subtract an nullable number from DateTime.now().year, which is not allowed.
You can workaround this limitation by using the ?? (Null coalescing) operator and checking for null and if your n is null subtract 0.
var age = DateTime.now().year - (n ?? 0) ;
Another solution is, just using an if checking for null followed by an else block, like:
if(n == null) {
print("Bad Year");
}
else {
var age = DateTime.now(). year - n ;
print(" ==> You are $age year old!");
}
Taking up your comment:
i added ! after n and works fine... that is the same solution?
Adding the null-assertion operator (!) to the n in DateTime.now().year - n! is not the same solution, but it is a possible solution. And as you already mentioned it compiles and work.
But be careful:
You are telling the compiler that you are sure, that n will never be null.
With your current code you will get an runtime error, when the user enters a letter or nothing. Then the parsing of that string fails and your n is null. And now you want to subtract null from the DateTime.now().year, which is again not allowed.
You can read more about Null-Safety on https://dart.dev/codelabs/null-safety .
Side note:
The following check is not correct:
if(n=="") {
print("Bad Year");
}
If an incorrect number was entered, your n is null and this is not equal to an empty string. To get your if clause work, simply use a null check. I also recommend to add a return after the output, otherwise your person gets quite old.
if(n == null) {
print("Bad Year");
return;
}

How to check a string null in Dart?

How do you check a string for "null" in Dart? (not a null object)
is there some Dart SDK API like Java's equals?
I believe isEmpty property will return false since if your string is null, it still holds an object and won't be empty. So depending on what you mean in your post.
If you want to check for the string 'null', just do
if (stringVar == 'null')
or if you want to check if your string is null, then
if (stringVar == null)
Checking if the string is null:
if (s == null) {
…
}
Checking if the string is not null:
if (s != null) {
…
}
Returning the string if not null, 'other value' otherwise:
return s ?? 'other value'
Assigning a value to the string only if that string is null:
s ??= 'value'
Calling a method (property) on the string if it's not null
s?.length
You can use the isEmpty property.
bool [string name] isEmpty;
Alternatively, you can do this:
String text = "text";
print(text.isEmpty);
Output: false
Edit: I believe that Mohammad Assad Arshad's answer is more accurate. Sorry about that.
In addition to
myString == 'null'
or
myString == null
There is another meaning that "null" could take: the null character. In ASCII or Unicode this has the code value of 0. So you could check for this value like so:
myString == '\u0000'
or
myString.codeUnits.first == 0