'Flutter http FormatException: Invalid number
I want to post data to an endpoint(woocommerce create order). I use the following code to do this:
var data = {
'customer_id': int.parse(Data.UserId),
'billing[first_name]': nameInputController.text,
'billing[phone]': phoneInputController.text,
'billing[postcode]': postalCodeInputController.text,
'billing[address_1]': addressInputController.text,
'shipping[first_name]': nameInputController.text,
'shipping[phone]': phoneInputController.text,
'shipping[postcode]': postalCodeInputController.text,
'shipping[address_1]': addressInputController.text,
'customer_note': note,
};
var url = Uri.parse(Endpoints.completeOrder);
var response = await http.post(url, body: data, headers: {
'Authorization': Data.authToken
});
but i get the following error:
I/flutter (25924): FormatException: Invalid number (at character 1)
I/flutter (25924):
I/flutter (25924): ^
When i replace int.parse(Data.UserId) with an string the problem is solved but i need to post customer_id as integer.
Solution 1:[1]
Since if you replace it with String the error is removed, try int.tryParse(Data.UserId).toString() to see if it fixes your issue. I would suggest using tryParse instead of parse since you can get a null default value if the parse fails.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | esentis |
