首页 > 解决方案 > 用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
  }
 }

标签: mysqlflutter

解决方案


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();

}

推荐阅读