我有以下來自firebase的串列:
[widget.recipeDocument['ingredients'] = 2 eggs, 2 tbsp milk, 30 g flour, 2 tbsp sugar, vanillaextract]
對于我的串列視圖,我將字串分成兩部分。第一部分是配料量,我根據用戶輸入的份量進行計算。第二部分是成分名稱。它適用于我有數量和成分名稱的成分。但正如你在我的清單中看到的,我也有沒有數量的“香草精”。我的串列視圖在這一點上中斷。我正在考慮使用 RegExpr 來檢測沒有空格的成分,但我不知道如何。
child: ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
primary: false,
itemCount:widget.recipeDocument['ingredients'].length,
itemBuilder: (context, index) {
final ingredient = widget.recipeDocument['ingredients'][index];
RegExp regExp = RegExp(" ");
print('REG EXPR = ${regExp.allMatches(ingredient).length}');
int splitt = ingredient.indexOf(" ");
String measure = ingredient.substring(0,splitt).trim();
String ingredient_name = ingredient.substring(ingredient.indexOf(" ") 1);
double x = double.parse(measure);
double calc = x * _counter;
return Card(
color: Colors.transparent,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25),
),
child:
ListTile(
dense: true,
visualDensity: VisualDensity(vertical: -3), // to compact
contentPadding: EdgeInsets.only(left: 15.0, right: 15.0),
tileColor: Colors.grey.withOpacity(0.2),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(25),
topRight: Radius.circular(25),
bottomRight: Radius.circular(25),
bottomLeft: Radius.circular(25))),
title:
Text('${(prettify(calc))} ${ingredient_name}',
style: GoogleFonts.nunito(color: Colors.white, fontSize: 18))
)
);
}
),
uj5u.com熱心網友回復:
我會這樣:
final split = ingredient.split(' ');
double calc = (double.tryParse(split[0]) ?? 0) * _counter;
String ingredient_name = calc == 0 ? ingredient : split.skip(1).join(' ');
然后讓文字成為
Text('${calc == 0 ? '' : '${prettify(calc)} '}$ingredient_name',
uj5u.com熱心網友回復:
您可以使用 hasMatch 函式 regExp 類來了解字串是否與您的正則運算式匹配:
var list = ["with space", "withoutspace"];
for(var elm in list)
{
var regEx = RegExp(r'\s');
var match = regEx.hasMatch(elm);
print("$elm, has space: $match");
}
結果 :
with space, has space: true
withoutspace, has space: false
我建議使用“\s”而不是“”,因為它會檢測所有形式的間距,只是為了安全!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/508533.html
