I started learning flutter and I'm currently working on the Simple CRUD operation project with SQFLITE. where i have Transaction Table in which there 6 columns (id,transactiontype,date,Category,Amount,notes).so, I want to get the sum of the total amount column with the help of the StreamBuilder. I tried but it is won't working for me somebody please help me out here, I'm so glad.....
HERE IS MY MODEL---
class TransactionModel {
int id;
String type;
String date;
String categoryid;
String amount;
String notes;
TransactionModel(
{this.id,
this.type,
this.date,
this.categoryid,
this.amount,
this.notes});
factory TransactionModel.fromDatabaseJson(Map<String, dynamic> data) =>
TransactionModel(
id: data['id'],
type: data['type'],
date: data['date'],
categoryid: data['categoryid'],
amount: data['amount'],
notes: data['notes']);
Map<String, dynamic> toDatabaseJson() => {
'id': this.id,
'type': this.type,
'date': this.date,
'categoryid': this.categoryid,
'amount': this.amount,
'notes': this.notes
};
}
HERE IS MY FUTURE FUNCTION TO CALCULATE THE SUM OF TOTAL OF AMOUNT COLUMN ----
Future getsumofIncome(String type) async {
final db = await dbProvider.database;
var incomeresult = await db.rawQuery(
"SELECT SUM(amount) FROM $transactionTABLE WHERE type = ?", ["$type"]);
int incomevalue = incomeresult[0]['SUM(amount)'];
print('Result of Income Sum Amount $incomevalue');
// print(incomeresult.toList());
return incomevalue;
}
HERE IS MY UI PART FOR SHOWING THE DATA FETCH FROM THE DATABASE FILE. I'm using the StreamBuilder for it but it doesn't work according to me..... Let me mentioned here I'm also using the bloc pattern here for my project I just put my logic function above, you check how I calculate the value...
stream: transactionBloc.getsumofIncome('Income'),
builder: (BuildContext context,
AsyncSnapshot<TransactionModel> incomesnapshot) {
if (incomesnapshot.hasData) {
return Container(
padding: EdgeInsets.all(10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Income',
style: TextStyle(fontSize: 20.0),
),
Text(
incomesnapshot != null
? '₹ ${incomesnapshot.toString()}'
: '0',
style:
TextStyle(fontSize: 20.0, color: Colors.green),
),
],
),
);
} else {
CircularProgressIndicator();
}
}),