我正在嘗試制作一個預算應用程式,其中每個預算都有自己的支出歷史。這些支出歷史中的每一個都有一個名為“budgetName”的變數,我可以使用如下 sqflite 代碼編譯并計算支出金額。
return await db.rawQuery("select sum(budgetSpent) as total from spending where budgetName ='" budgetTitle "'");
.then((value) {print(value);})如果我在呼叫 sqflite 函式時嘗試使用 a并在除錯控制臺中查看每個預算支出的值,則此方法有效。
但問題是我在呼叫函式時需要'budgetTitle',以便它可以與支出的'budgetName'進行比較以獲得總支出金額。
所以我現在所擁有的是我嘗試獲得如下支出金額:
child: BudgetCard(
budgetName: budget.budgetName,
budgetSpent: '${Provider.of<SpendingDatabaseHelper>(context, listen: false).getSpecificSpending(budget.budgetName}',
maxBudget: currency.format(int.parse(budget.maxBudget)),
svgIcon: iconListBudgetCards[budget.iconValue],
color: colorSwatch[budget.colorValue],
percentage: 0.5),
),
但它只會回傳,Instance of 'Future<dynamic>'因為它在獲取值之前需要“等待”。但我找不到另一種方法,因為它需要傳遞“預算標題”。
非常感謝任何幫助、想法或建議!先感謝您。
下面是資料庫代碼:
String? budgetSpendingAmount;
getSpecificSpending(budgetTitle) async {
dynamic result =
await SpendingDatabaseHelper.instance.getSpendingAmount(budgetTitle);
String a = result.toString();
debugPrint('A: $a');
if (a == '[{total: null}]') {
a = currency.format(int.parse('000'.trim()));
budgetSpendingAmount = a;
print(budgetSpendingAmount);
} else {
String? b = a.replaceAll(RegExp(r'[{\}\[\]\-] '), '');
String c = b.substring(b.indexOf(":") 1);
budgetSpendingAmount = currency.format(int.parse(c.trim()));
}
notifyListeners();
}
Future getSpendingAmount(String budgetTitle) async {
Database db = await instance.database;
return await db.rawQuery("select sum(budgetSpent) as total from spending where ='" budgetTitle "'");
}
這是我呼叫該函式以獲取支出金額資料的完整代碼:
Widget build(BuildContext context) {
return FutureBuilder<List<Budget>>(
future: Provider.of<BudgetDatabaseHelper>(context).getBudgets(),
/// Displaying the data from the list
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const Center();
}
return snapshot.data!.isEmpty
? const Flexible(
child: Center(
child: Padding(
padding: EdgeInsets.only(bottom: 80.0),
child: Text(
'You don\'t have any budget',
style: kCaption,
),
)))
: Flexible(
child: ListView.builder(
physics: const BouncingScrollPhysics(),
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
final budget = snapshot.data![index];
return Dismissible(
key: UniqueKey(),
background: const Align(
alignment: Alignment.centerRight,
child: Padding(
padding: EdgeInsets.only(bottom: 12.0, right: 24),
child: Icon(
IconlyLight.delete,
color: cRed,
size: 24,
),
),
),
direction: DismissDirection.endToStart,
onDismissed: (direction) {
snapshot.data!.removeAt(index);
Provider.of<BudgetDatabaseHelper>(context,
listen: false)
.removeMethod(budget.id!, budget.budgetName);
},
child: GestureDetector(
onTap: () => showModalBottomSheet(
backgroundColor: Colors.transparent,
context: context,
enableDrag: true,
isScrollControlled: true,
builder: (context) {
return DraggableScrollableSheet(
snap: true,
minChildSize: 0.43,
maxChildSize: 0.85,
initialChildSize: 0.43,
snapSizes: const [0.43, 0.85],
builder: (context, scrollController) {
return ClipRRect(
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(32),
topRight: Radius.circular(32)),
child: Container(
color: cWhite,
child: SingleChildScrollView(
controller: scrollController,
physics: const BouncingScrollPhysics(),
child: BudgetDetails(
id: budget.id!,
budgetName: budget.budgetName,
budgetSpent: 'budgetSpent',
colorValue:
colorSwatch[budget.colorValue],
maxBudget: currency.format(
int.parse(budget.maxBudget)),
svgIcon: iconListBudgetDetails[
budget.iconValue],
),
),
),
);
},
);
},
),
child: BudgetCard(
budgetName: budget.budgetName,
budgetSpent: '${Provider.of<SpendingDatabaseHelper>(context, listen: false).getSpecificSpending(budget.budgetName}',
maxBudget: currency.format(int.parse(budget.maxBudget)),
svgIcon: iconListBudgetCards[budget.iconValue],
color: colorSwatch[budget.colorValue],
percentage: 0.5),
),
);
},
),
);
},
);
}
uj5u.com熱心網友回復:
使用provider的widget樹是不是一個好主意。做一個statefullWidget
SpendingDatabaseHelper像這樣在你的身上做一個吸氣劑
String? _budgetSpendingAmount;
String get budgetSpendingAmount=> _budgetSpendingAmount;
并像這樣初始化它_budgetSpendingAmount = currency.format(int.parse(c.trim()));
所以使用這個getter,你可以在小部件樹的任何地方訪問這個值
Future<void> _getSpecificSpending(String budgetName)async{
try{
await Provider.of<SpendingDatabaseHelper>(context, listen: false).getSpecificSpending(budgetName);
} catch(e){
print('error :$e');
}
}
在你的小部件樹中寫下這樣的東西
child: FutureBuilder(
future : _getSpecificSpending(budget.budgetName)
builder: (ctx,snapshot){
var spendDataProv=Provider.of<SpendingDatabaseHelper>(context, listen: false);
return snapshot.connectionState==ConnectionState.waiting ?
CircularProgressIndicator() :
BudgetCard(
budgetName: budget.budgetName,
budgetSpent:spendDataProv.budgetSpendingAmount ,
maxBudget: currency.format(int.parse(budget.maxBudget)),
svgIcon: iconListBudgetCards[budget.iconValue],
color: colorSwatch[budget.colorValue],
percentage: 0.5)
},
)
uj5u.com熱心網友回復:
一些想法
在 BudgetCard 小部件中使用 FutureBuilder。然后,當您仍在等待未來完成時,您可以顯示一個 CircularProgressIndicator 花費的金額。
或者
使用一個布爾標志(在未來方法的開始和結束時翻轉)來指示未來是否完成。flag false:顯示progressIndicator,flag true 顯示已花費金額。
或者
呼叫時Provider.of<BudgetDatabaseHelper>(context).getBudgets(),您可以讓該方法getBudgets()也用您稍后需要的資訊填充陣列。因此,在您擁有的每個budgetNameProvider.of<SpendingDatabaseHelper>(context, listen: false).getSpecificSpending(budget.budgetName)的方法內部呼叫。getBudgets()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/421012.html
標籤:
上一篇:未來函式無限重新加載?撲
