Converting a string to double in Dart using double.parse() - google-api

I am trying to convert coordinates returned from the Google Geo. API into doubles. A simplified part of the response that gets returned is...
{
locationData: {
bounds: {
northeast: 40.222222,
southwest: 38.265987
}
}
}
When I use print(locationData['bounds']['northeast']) my console reads that and understands that the value is 40.222222 However, when I try to use a parse method, I get the following error:
var neLat = double.parse(locationData['bounds']['northeast']);
I/flutter (10537): type 'double' is not a subtype of type 'String' in type cast where
I/flutter (10537): double is from dart:core
I/flutter (10537): String is from dart:core
That error leads me to believe that my parse is not valid, even though it is clearly a double value, just as a string. Am I missing something for this conversion? I have seen conversion in multiple examples using double.parse, I just can't figure out what my issue is..

If the value was parsed from JSON, Dart knows the value is a double and the error is telling me that the value is of type double and the convert expected type string.
So the error is much more clear once I understood!

I was too facing same issue.
I know it is a silly answer but it worked for me. I was using Getx to save states and while converting string to double it was displaying error
String is not subtype of type double
as the state of previous variables was being saved, so I restarted the app and tried type casting and it worked well.

Related

Postman doesn't get correct type of Int variable in a GraphQL call

I've set the body of request like this:
The call return me this error message:
"Variable "$idCentralina" got invalid value "1"; Expected type Int. Int cannot represent non-integer value: "1""
I don't understand why it get idCentralina like a string if in the declaration when I use an Integer (without quote) to initialize it.
In gql schema the query is this:
getCoseByCentralina(
idCentralina: Int
date: String
): [Cose]
Does someone know the cause? and explain to me why this happens.
UPDATE
If I insert the Int value in the query without using the variables it works.
Hopefully somebody knows the cause of the problem of postman variables.
EDIT - SOLUTION OF MY CASE
I find the problem, a custom middleware of backend converts all req.body params into String type, I don't know why.

A non well formed numeric value encountered in laravel

I am trying to add two variables together. I believe both contain an integer, but when I draw what is stored within $product->mileage, I receive the following error:
A non well formed numeric value encountered
$oilchange = $request->oilchange_at_kms;
$product = Product::find($request->product_id);
$mileage = $product->mileage; // Error within this variable, but it is an int
$total = $mileage + $oilchange;
How can I test this, or how can I find the problem in my code?
This error usually pops up when you try to add an integer with a string or some type of non numeric field.
You can test this by using the PHP gettype() method:
dump(gettype($product->mileage));
dd(gettype($oilchange));
If it turns out that one of these is a string (possibly from a form response), you can cast it to an int if you are certain that the value will always be an int.
$mileage = (int)$product->mileage;
Not really recommending this, as you should try to resolve the types within the variables first, but it may help you in testing.

How does one convert Local<Name> to a string?

So after being away awhile some things changed. I used to be able to do this:
void ObjectTemplateProxy::GetProperty(Local<String> hName, const PropertyCallbackInfo<Value>& info)
{
auto hStr = hName->ToString();
But now I need an isolate, and the parameter type changed to Local<Name>. Then I tried this:
auto hStr = hName->ToString(info.GetIsolate());
But this still doesn't work because hStr is null. It is null because, as it turns out, the name is actually a Symbol type and NOT String. I don't see any way to convert a symbol to a string, which I need to do to send the name to the C# CLR via P/Invoke to pull the value from a dictionary using string keys.
So, it would seem that there is a Name() function on the Symbol type that returns a String. I was looking for a function on the Symbol type and didn't find one (must have missed it). The code that worked was hName.As<Symbol>()->Name().As<String>();.
As noted below in the comments, Symbol() is not required to have a name, so beware!

Why is Integer.MIN_VALUE seen as a double by nashorn?

I pass Integer.MIN_VALUE in some Javascript that just returns the input. However, it seems that when I compare the result of the Javascript by using ScriptValueConverter.unwrapValue(returnedObject), the value is the same but the type is a double while I am expecting an integer.
However when I pass Integer.MAX_VALUE in, I do get an integer.
From what I understand, Nashorn uses optimistic type casting so in my eyes, MIN_VALUE can still be an int, there's no need for it to be a double
I suppose that the problem is not Nashorn itself but the javascript code you are running or the third party converter you are using (ScriptValueConverter is neither a javax.script nor a jdk.nashorn class).
I've run the following code:
ScriptEngine jsEngine = new ScriptEngineManager().getEngineByName("nashorn");
jsEngine.eval("function identity(x) {return x;}");
System.out.println("Integer.MIN_VALUE:" + ((Invocable) jsEngine).invokeFunction("identity", Integer.MIN_VALUE).getClass());
System.out.println("Integer.MAX_VALUE:" +((Invocable) jsEngine).invokeFunction("identity", Integer.MAX_VALUE).getClass());
and the output is:
Integer.MIN_VALUE: class java.lang.Integer
Integer.MAX_VALUE: class java.lang.Integer
Here you can find a brief explanation about nashorn type conversions.

string was not recognized as a valid datetime. in c#

i am using this code for passing datetime.
Convert.ToDateTime(textBox1.Text)
but the compiler show an error that string was not recognized as a valid datetime. so how to avoid this error and is a datetime field in my database.
Convert.ToDateTime(textBox1.Text)
This code will throw an exception if the string value in textBox1.Text doesn't represent a valid DateTime (or at least can't be parsed into one with default functionality in C#). You can add some defensive programming to handle errors like this.
The DateTime type (as well as most, if not all, common value types in .NET) has a method on it called TryParse() specifically for the purpose to attempting to parse a value into that type without throwing an exception. The method returns true if the parse was successful, false otherwise. And it accepts an out parameter to hold the resulting parsed value (or the original value if parsing is unsuccessful).
So instead of this:
var dateTimeValue = Convert.ToDateTime(textBox1.Text);
You could use something like this:
var dateTimeValue = DateTime.MinValue;
if (DateTime.TryParse(textBox1.Text, out dateTimeValue))
// use the value for something

Resources