我无法更新数据,每次都重新添加产品,我想更新


class DetailedCountingScreen extends StatefulWidget {
final String countDate;
final String standName;
const DetailedCountingScreen(
{required this.countDate, required this.standName});
@override
_DetailedCountingScreenState createState() => _DetailedCountingScreenState();
}
class _DetailedCountingScreenState extends State<DetailedCountingScreen> {
final firebaseRef = FirebaseDatabase.instance.reference();
List<String> productNames = [];
List<int> soldPieces = [];
List<int> countedPieces = [];
List<double> productPrices = [];
@override
void initState() {
firebaseRef
.child('Sayımlar')
.child(widget.standName)
.child('${widget.countDate}')
.onValue
.listen((event) {
final snapshot = event.snapshot.value ?? {};
final countedProductsMap = new Map<String, dynamic>.from(snapshot);
countedProductsMap.forEach((key, value) {
setState(() {
productNames.add(value['ürünİsmi']);
soldPieces.add(value['satılanMiktar']);
countedPieces.add(value['sayılanMiktar']);
productPrices.add(value['ürünFiyatı']);
});
});
});
super.initState();
}
Widget countsCard(
{required int soldPiece,
required double productPrice,
required int countedPiece,
required String productName,
required int thePieceBeforeCount,
required double totalPrice}) {
return Card(
color: Colors.white,
child: ListTile(
isThreeLine: true,
title: Text(
productName.toUpperCase(),
style: kBlackTS.copyWith(fontWeight: FontWeight.bold),
),
subtitle: Column(
children: [
Row(
children: [
Text('Sayılan Adet: $countedPiece', style: kBlackTS),
emptySpaceWidth(context, 0.04),
Text('Satılan Adet: $soldPiece', style: kBlackTS),
emptySpaceWidth(context, 0.04),
Text('Sayım Öncesi Adet: $thePieceBeforeCount',
style: kBlackTS),
],
),
emptySpaceHeight(context, 0.015),
Row(
children: [
Text('Satış Fiyatı: ${productPrice.toStringAsFixed(2)} ₺',
style: kBlackTS),
emptySpaceWidth(context, 0.04),
Text('Toplam Tutar: ${totalPrice.toStringAsFixed(2)} ₺',
style: kBlackTS),
],
),
],
),
),
);
}
FloatingActionButton get _invoiceFAB {
return FloatingActionButton(
backgroundColor: Colors.blue.shade700,
child: Icon(FontAwesomeIcons.fileInvoice, color: Colors.white),
onPressed: () {});
}
@override
Widget build(BuildContext context) {
CustomAppBar customAppBar = CustomAppBar(
context: context,
colour: Colors.blue.shade700,
title: '${widget.countDate} Tarihli Sayımlar');
return SafeArea(
child: Scaffold(
backgroundColor: Colors.blue.shade100,
appBar: customAppBar.customAppBar,
body: Container(
child: Column(
children: [
ListView.builder(
shrinkWrap: true,
itemCount: productNames != [] ? productNames.length : 0,
itemBuilder: (context, index) {
double total = soldPieces[index] * productPrices[index];
int thePieceBeforeCount =
soldPieces[index] + countedPieces[index];
return countsCard(
soldPiece: soldPieces[index],
productPrice: productPrices[index],
countedPiece: countedPieces[index],
productName: productNames[index],
thePieceBeforeCount: thePieceBeforeCount,
totalPrice: total);
},
),
],
),
),
floatingActionButton: _invoiceFAB,
),
);

我不想更新数据,而是重新添加数据,你能帮我如何修复吗?我应该改变什么,代码应该是怎样的?我想更新我添加的产品,这是我作业需要的,我不知道该怎么做。我需要帮助什么代码写更新提前谢谢你

  • 避免在initState方法中检索firebase数据
  • 根据您的要求使用SteamBuilder或FutureBuilder
  • 我更喜欢SteamBuilder为您的情况
  • 参考文档获取更多参考https://api.flutter.dev/flutter/widgets/StreamBuilder-class.html
  • 参考从firebase获取和维护https://stackoverflow.com/a/65960948/14950155
  • 的示例

最新更新