如何在文本小部件中快速显示总价

时间:2020-06-17 20:26:53

标签: flutter sum sqflite

我想用tezt小部件在flutter ui中显示总价格。我尝试过原始查询

var sonuc=await db.rawQuery("SELECT SUM(gelirTutar) FROM gelirIslem");

它返回了QueryResultSet结果[{SUM(gelirTutar):70.0}],并且产生了一个错误Unhandled Exception:type'List>'不是'String'类型的子类型

如何在文本小部件中显示“ 70.0”数据

这段代码是我的求和方法

Future gelirTopla()async{
    var db=await _getDatabase();
    var sonuc=await db.rawQuery("SELECT SUM(gelirTutar) FROM gelirIslem");

   print("gelir:"+sonuc.toString());
    return sonuc;
  }

和此代码UI设计

class KasaIslem extends StatefulWidget {
  @override
  _KasaIslemState createState() => _KasaIslemState();
}

class _KasaIslemState extends State<KasaIslem> {
  double toplamGelir;
  DatabaseHelper databaseHelper;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    databaseHelper=DatabaseHelper();
    setState(() {
      gelirHesapla();

    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Kasa İşlemleri"),
        ),
        body: Column(
          children: <Widget>[


                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Text(
                                "Günlük Gelir:",
                                style: TextStyle(color: Colors.redAccent),
                              ),
                            ),
                            Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Text(
                                toplamGelir.toString(),
                                style: TextStyle(color: Colors.redAccent),
                              ),
                            ),
                          ],
                        ),

                      ],
                    ),
                  );


  }

  void gelirHesapla() async{
    var toplam=await databaseHelper.gelirTopla();
    setState(() {
      print("ttt"+toplam.toList());
      toplamGelir=toplam;
    });


  }
}

1 个答案:

答案 0 :(得分:0)

rawQuery始终返回地图列表,因此您需要提取字符串。如果您将查询更新为:

var sonuc=await db.rawQuery("SELECT SUM(gelirTutar) as sum FROM gelirIslem");

然后结果应为[{sum: 70.0}],因此您可以执行sonuc.first["sum"]来获取计数。