Unhandled Exception: type 'int' is not a subtype of type 'String' - flutter

I have this error : Unhandled Exception: type 'int' is not a subtype of type 'String' on this line of code :
double d=double.parse(map2["gain_gem"]);
map2["gain_gem"] is returned by php script api and return value 1
And after i try to do this :
globals.points = globals.points+d;
i have tried too :
globals.points = globals.points+double.parse(map2["gain_gem"]);
same problem
globals.points is defined as double in my script
double points=0;

You need to setup the int as a String first. The map2 value "gain_gem" is an int, that's where your error is coming from but to parse that value as a double it needs to be a String so if you toString the input it will be able to change it to a double. You should definitely make sure it will always be an int of course.
double d=double.parse(map2["gain_gem"].toString());
Documentation on toString()

Related

How to deserialize 1 and 0 integer to bool in flutter built value

I'm using built_value package to deserialize json responses from server.
but the boolean properties are returned as integer that is 1 (for true) or 0 (for false).
when I try to deserialize them to boolean, I get this error:
failed due to: Deserializing '...' to 'ActiveSymbolResponse' failed due to: Deserializing
'0' to 'bool' failed due to: type 'int' is not a subtype of type 'bool' in type cast
How can I deserialize integer to boolean using built_value package?

Couldn't infer type parameter 'T'. when using Map.reduce(max)

I want to get the max value from a List. But since i migrated to null safety my code doesnt work anymore:
weightChart = [45.3, 21.5, 521.3];
weightChart.reduce(max)
I get the error:
Couldn't infer type parameter 'T'. Tried to infer 'double?' for 'T' which doesn't work: Type
parameter 'T' is declared to extend 'num' producing 'num'. The type 'double?' was inferred from:
Function type declared as 'T Function<T extends num>(T, T)' used where 'double? Function(double?,
double?)' is required. Consider passing explicit type argument(s) to the generic.
Let's try
import 'dart:math';
void main() {
List<double> weightChart = [45.3, 21.5, 521.3];
print(weightChart.reduce(max));
}

Flutter Problem about Constructor return type

The constructor returns type 'dynamic' that isn't of expected type 'Widget'.dart(invalid_cast_new_expr)

Converting Double To Int using type casting in swift?

I want to convert Double to Int using Type Casting.
This is my code:
let number:Int = 3.0 as Int
It is showing an error:
Cannot convert value of type 'Double' to type 'Int' in coercion
How can I solve this?
You can use only Int type to solve this issue
And I think the better way is not to cast from Double to Int, just create new value of Int type with Int(value: Double)
You need to use the Int() initializer.
Int and Double are completely unrelated types. Writing someDouble as Int is exactly as invalid as writing someViewController as Character.
In other C-like languages, the cast operator is dual purpose. It does both casts and conversions. Downcasting an object from a super type to a sub type does a type check, but performs no actual change of the value. But a conversion from double to int calls a function to do the conversion, that completely changes around the bits.
Swift's casting operator is more focused. It casts. For anything else, we use initializers.

Swift: Cannot convert value of type 'Int?' to specified type 'UInt32'

I'm trying to assign an array count to a UInt32. I get the error "Cannot convert value of type 'Int?' to specified type 'UInt32'". The array count is type Int, but the error says "Int?", which looks like an optional Int. I have no idea what else it could mean.
let randColorCount:UInt32 = slider?.randUIColors.count
I've also tried:
let randColorCount:UInt32 = UInt32(slider?.randUIColors.count)
But I get the error "Cannot invoke initializer for type 'UInt32' with an argument list of type '(Int?)'".
slider?.randUIColors.count results in Int? because of the use of the optional chaining.
One simple solution is to combine this with the nil coalescing operator ?? to provide a default value incase slider is nil.
let randColorCount = UInt32(slider?.randUIColors.count ?? 0)