mysql - 用mysql计算flutter的总价
问题描述
嘿伙计们,我需要一些帮助我无法在结帐过程中计算总价我正在从我的 mysql 服务器获取数据。我已经尝试了很多事情,但我无法解决这个问题。有没有人可以帮助我?
returnTotalAmount 函数由 BottomBar 小部件包装,它只是 UI,因此我没有分享它。
这是我包装的小部件代码
home: new Scaffold(
appBar: new AppBar(title: const Text('MySQL Images Text')),
body: new Center(
//FutureBuilder is a widget that builds itself based on the latest snapshot
// of interaction with a Future.
child: new FutureBuilder<List<Employee>>(
future: Services.getEmployees(),
//we pass a BuildContext and an AsyncSnapshot object which is an
//Immutable representation of the most recent interaction with
//an asynchronous computation.
builder: (context, snapshot) {
if (snapshot.hasData) {
List<Employee> _employee = snapshot.data;
Services.getEmployees();
return Scaffold(
body: SafeArea(
child: CustomListView(_employee),
),
bottomNavigationBar: BottomBar(_employee),
);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
String returnTotalAmount(List<Employee> _employee) {
double totalAmount = 0.0;
for (int i = 0; i < _employee.length; i++) {
totalAmount = totalAmount + _employee[i].price * _employee[i].quantity;
}
return totalAmount.toRadixString(2);
}
Container totalAmount(List<Employee> _employee) {
return Container(
margin: EdgeInsets.only(right: 10),
padding: EdgeInsets.all(25),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
" Total:",
style: TextStyle(fontSize: 25, fontWeight: FontWeight.w300),
),
Text(
"\$${returnTotalAmount(_employee)}",
style: TextStyle(fontWeight: FontWeight.w700, fontSize: 28),
),
],
),
);
}
这是我的结帐页面,我应该计算价格
this is my Employee Page
class Employee{
String id;
String firstName;
String price;
String path;
String quantity;
Employee({this.id, this.firstName,this.price,this.path,this.quantity});
factory Employee.fromjson(Map<String, dynamic> json){
return Employee(
id: json['id'] as String,
firstName: json['first_name'] as String,
price: json['price'] as String,
path: json['path'] as String,
quantity: json['quantity'] as String,
);
}}
这是我从数据库获取数据的api代码
static Future<List<Employee>> getEmployees() async {
try {
var map = Map<String, dynamic>();
map['action'] = _GET_ALL_ACTION;
final response = await http.post(ROOT, body: map);
print('getEmployees Response: ${response.body}');
if (200 == response.statusCode) {
List<Employee> list = parseResponse(response.body);
return list;
} else {
return List<Employee>();
}
} catch (e) {
return List<Employee>(); // return an empty list on exception/error
}
}
解决方案
String returnTotalAmount(List _employee) { double totalAmount = 0.0;
for (int i = 0; i < _employee.length; i++) {
totalAmount = totalAmount + (double.parse(_employee[i].price)*double.parse(_employee[i].quantity));
}
return totalAmount.toString();
}
推荐阅读
- awk - 只有当它们在一系列文件中一个接一个地出现时,有没有办法提取两个连续的记录?
- python - 遍历对结果和下一个值进行操作的列表
- r - 使用 ggplot 和 shiny 获取条形图中的特定值时出错
- bixby - 防止值编译错误
- php - Codeigniter 4 cookie 问题无法设置
- javascript - 将 JS 测验的正确答案设为绿色,将错误答案设为红色
- java - Java 中 x + x 和 2*x 的区别
- python - REST API url 设计
- python - 使用 Plotly 和 Flask 的雷达图
- postgresql - 如何设置创建某些行的日期?我试图在 PostgreSQL 中只使用触发器和函数