Skip to content
Mahgoub

A Software Developer who does not let his schooling interfere with his education

Mahgoub

A Software Developer who does not let his schooling interfere with his education

Tip: Parsing doubles and integers in Dart

mmahgoub, October 18, 2023February 20, 2024

In order to gracefully handle potential exceptions when parsing a string into a double, one effective technique is to prepend a "0" to the input string before performing the conversion. By employing this approach, the default value becomes zero, ensuring that no exceptions are raised even when the input is empty or cannot be successfully converted into a numeric value. This method provides a robust and fault-tolerant way to work with user input and is particularly useful in scenarios where a default value of zero is acceptable in case of invalid or empty input.

Example:
double.parse("0${textfieldController.value.text}");

While this solution effectively prevents exceptions and is a practical choice for situations where a default of zero suffices, it is essential to consider the specific context and requirements of the application. If the desired default value is different from zero or if the application necessitates user-friendly feedback for invalid input, it may be worthwhile to explore alternative error-handling strategies. Ultimately, the choice to use this "0"-prefix technique should be made with careful consideration of the overall user experience and the application’s specific needs.

Blog DartFlutter

Post navigation

Previous post
Next post

Related Posts

Blog

Bucket List

September 4, 2023February 20, 2024

Do you ever find yourself caught in the whirlwind of life, trying to keep up with the ever-evolving world of technology? Despite our best efforts to stay current in our fields, it’s easy to slowly drift and miss out on exciting opportunities for growth and learning. That’s exactly where I…

Read More
Blog

Adding those to my reading list

November 26, 2023February 20, 2024

I especially looking forward to reading uncle Bob’s Clean Architecture and Eric Evan’s Domain-Driven Design which is not in this picture.

Read More
Articles

Taming the Legacy Beast: Setting Up an Odoo 10 Development Environment

December 14, 2023February 20, 2024

Dealing with legacy code always adds an interesting twist to the development process. Over the past few days, I’ve embarked on setting up a development environment for a legacy Odoo 10 modules codebase (mind you, Odoo 17 is already out as I write this). Let me tell you, it’s been…

Read More
©2026 Mahgoub | WordPress Theme by SuperbThemes