Skip to content

Commit bdfccff

Browse files
fix: Nutrition extraction fix (#6469)
* fix: format extraction values properly and ignore non numerical extractions * fix: update extraction value handling and improve number formatting * Removed comment * Fixed typo
1 parent f3af118 commit bdfccff

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

packages/smooth_app/lib/pages/product/nutrition_page/widgets/nutrition_facts_editor.dart

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ class _NutrientRowState extends State<NutrientRow>
9696
PerSize.oneHundredGrams,
9797
);
9898

99+
String? extractionValue = robotoffNutrientEntity?.value;
100+
101+
// We need to make sure the value is formatted properly
102+
// We also ignore text extractions such as "traces" until the API is ready
103+
if (extractionValue != null) {
104+
final num? extractionValueNum = NumberFormat().tryParse(extractionValue);
105+
if (extractionValueNum == null) {
106+
extractionValue = extractionValueNum.toString();
107+
}
108+
}
109+
99110
return ColoredBox(
100111
color: color,
101112
child: Padding(
@@ -143,8 +154,7 @@ class _NutrientRowState extends State<NutrientRow>
143154
),
144155
],
145156
),
146-
if (robotoffNutrientEntity?.value != null &&
147-
robotoffNutrientEntity!.value! != controller.text)
157+
if (extractionValue != null && extractionValue != controller.text)
148158
Container(
149159
margin: const EdgeInsetsDirectional.only(
150160
bottom: SMALL_SPACE,
@@ -167,7 +177,7 @@ class _NutrientRowState extends State<NutrientRow>
167177
),
168178
),
169179
Text(
170-
robotoffNutrientEntity.text!,
180+
extractionValue,
171181
style: TextStyle(
172182
color: extension.success,
173183
fontWeight: FontWeight.bold,
@@ -178,8 +188,8 @@ class _NutrientRowState extends State<NutrientRow>
178188
.edit_product_form_item_add_suggestion,
179189
child: IconButton(
180190
onPressed: () {
181-
controller.text = robotoffNutrientEntity.value!;
182-
final Unit? unit = robotoffNutrientEntity.unit;
191+
controller.text = extractionValue!;
192+
final Unit? unit = robotoffNutrientEntity?.unit;
183193
if (unit != null) {
184194
widget.nutritionContainer.setNutrientUnit(
185195
widget.orderedNutrient.nutrient!,
@@ -307,7 +317,8 @@ class _NutrientValueCell extends StatelessWidget {
307317
inputFormatters: <TextInputFormatter>[
308318
FilteringTextInputFormatter.allow(
309319
SimpleInputNumberField.getNumberRegExp(
310-
decimal: true),
320+
decimal: true,
321+
),
311322
),
312323
DecimalSeparatorRewriter(decimalNumberFormat),
313324
],

0 commit comments

Comments
 (0)