Flutter Firestore 计算不起作用
Flutter Firestore calculations not working
Objective
我有一个 Firestore 应用程序可以在文档中记录项目及其价值。我想 return 所有这些文件的 "Value" 字段并计算所有文件的总价值(加起来)。
问题
当我使用下面的代码行时,它只是一个一个地打印值(见附件截图)。我还附上了我的 Firestore 结构的屏幕截图。我试过使用 sum 和 sum2 但两者 return 结果相同。
var getValueSTRING = documentSnapshot['Value'];
var one = int.parse(getValueSTRING);
var sum = [values].reduce((curr, next) => curr + next);
var sum2 = list2.fold(0, (curr, next) => curr + next);
print(sum);
产生的结果
Reloaded 3 of 579 libraries in 446ms.
I/flutter ( 3161): [100]
I/flutter ( 3161): [200]
代码
class Expenses extends StatefulWidget {
@override
ESTATE createState() => ESTATE();
}
class ESTATE extends State<Expenses> {
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: Firestore.instance
.collection('users')
.document(userDocPATH.toString())
.collection(userCardPATH.toString())
.document(selectedCardPATH)
.collection('Expenses')
.snapshots(),
builder: (context, snapshot) {
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot documentSnapshot =
snapshot.data.documents[index];
var getValueSTRING = documentSnapshot['Value'];
var one = int.parse(getValueSTRING);
var list2 = [one];
var sum2 = list2.fold(0, (curr, next) => curr + next);
print(sum2);
return Text(documentSnapshot['Value']);
});
});
}
}
假设您准备好了 QuerySnapshot snapshot
。您可以像这样计算所有值的总和:
final sum = snapshot.data.documents.fold(0, (prev, next) => prev + int.parse(next['Value']))
也就是说折叠应该在List<DocumentSnapshot>
.
上进行
根据更新的代码进行编辑:
罪魁祸首是这些行:
var one = int.parse(getValueSTRING);
var list2 = [one];
var sum2 = list2.fold(0, (curr, next) => curr + next);
问题是代码试图折叠只有一个值的列表。包含一项的总和列表将始终净额等于该项的值。
要获得正确的值,需要考虑所有 DocumentSnapshots
。目前,仅考虑一个 DocumentSnapshot
,如以下行所示:
DocumentSnapshot documentSnapshot = snapshot.data.documents[index];
相反,尝试这样的事情(我省略了不必要的代码以减少视觉噪音):
<...>
builder: (context, snapshot) {
// Add this line to calculate total sum, and use it later on
final sum = snapshot.data.documents.fold(0.0, (prev, next) => prev + int.parse(next['Value']))
return ListView.builder(<...>);
编辑 2(工作代码):
假设以下 Firebase 数据库结构:
Expenses > doc1 > Value: "100"
doc2 > Value: "200"
doc3 > Value: "300"
下面的代码计算总和并将其显示为 header。它还显示每个单独的值。只要 Firebase 结构相同,代码就应该 运行 并产生正确的结果。添加新文档或编辑现有文档将同时影响条目列表和总和。
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
void main() {
runApp(Expenses());
}
class Expenses extends StatefulWidget {
@override
ESTATE createState() => ESTATE();
}
class ESTATE extends State<Expenses> {
final collection = Firestore.instance.collection('Expenses');
@override
Widget build(BuildContext context) {
return MaterialApp(
home: StreamBuilder<QuerySnapshot>(
stream: collection.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return CircularProgressIndicator();
final documents = snapshot.data.documents;
final sum = documents.fold(0, (s, n) => s + int.parse(n['Value']));
return Scaffold(
appBar: AppBar(title: Text("Total sum: $sum")),
body: ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
final documentSnapshot = snapshot.data.documents[index];
final value = int.parse(documentSnapshot['Value']);
return Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Document $index: $value'),
);
},
),
);
},
),
);
}
}
最终合集=Firestore.instance.collection('Expenses');
// 如何获取 uid 然后获取 docs???
Objective
我有一个 Firestore 应用程序可以在文档中记录项目及其价值。我想 return 所有这些文件的 "Value" 字段并计算所有文件的总价值(加起来)。
问题
当我使用下面的代码行时,它只是一个一个地打印值(见附件截图)。我还附上了我的 Firestore 结构的屏幕截图。我试过使用 sum 和 sum2 但两者 return 结果相同。
var getValueSTRING = documentSnapshot['Value'];
var one = int.parse(getValueSTRING);
var sum = [values].reduce((curr, next) => curr + next);
var sum2 = list2.fold(0, (curr, next) => curr + next);
print(sum);
产生的结果
Reloaded 3 of 579 libraries in 446ms.
I/flutter ( 3161): [100]
I/flutter ( 3161): [200]
代码
class Expenses extends StatefulWidget {
@override
ESTATE createState() => ESTATE();
}
class ESTATE extends State<Expenses> {
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: Firestore.instance
.collection('users')
.document(userDocPATH.toString())
.collection(userCardPATH.toString())
.document(selectedCardPATH)
.collection('Expenses')
.snapshots(),
builder: (context, snapshot) {
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot documentSnapshot =
snapshot.data.documents[index];
var getValueSTRING = documentSnapshot['Value'];
var one = int.parse(getValueSTRING);
var list2 = [one];
var sum2 = list2.fold(0, (curr, next) => curr + next);
print(sum2);
return Text(documentSnapshot['Value']);
});
});
}
}
假设您准备好了 QuerySnapshot snapshot
。您可以像这样计算所有值的总和:
final sum = snapshot.data.documents.fold(0, (prev, next) => prev + int.parse(next['Value']))
也就是说折叠应该在List<DocumentSnapshot>
.
根据更新的代码进行编辑:
罪魁祸首是这些行:
var one = int.parse(getValueSTRING);
var list2 = [one];
var sum2 = list2.fold(0, (curr, next) => curr + next);
问题是代码试图折叠只有一个值的列表。包含一项的总和列表将始终净额等于该项的值。
要获得正确的值,需要考虑所有 DocumentSnapshots
。目前,仅考虑一个 DocumentSnapshot
,如以下行所示:
DocumentSnapshot documentSnapshot = snapshot.data.documents[index];
相反,尝试这样的事情(我省略了不必要的代码以减少视觉噪音):
<...>
builder: (context, snapshot) {
// Add this line to calculate total sum, and use it later on
final sum = snapshot.data.documents.fold(0.0, (prev, next) => prev + int.parse(next['Value']))
return ListView.builder(<...>);
编辑 2(工作代码):
假设以下 Firebase 数据库结构:
Expenses > doc1 > Value: "100"
doc2 > Value: "200"
doc3 > Value: "300"
下面的代码计算总和并将其显示为 header。它还显示每个单独的值。只要 Firebase 结构相同,代码就应该 运行 并产生正确的结果。添加新文档或编辑现有文档将同时影响条目列表和总和。
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
void main() {
runApp(Expenses());
}
class Expenses extends StatefulWidget {
@override
ESTATE createState() => ESTATE();
}
class ESTATE extends State<Expenses> {
final collection = Firestore.instance.collection('Expenses');
@override
Widget build(BuildContext context) {
return MaterialApp(
home: StreamBuilder<QuerySnapshot>(
stream: collection.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return CircularProgressIndicator();
final documents = snapshot.data.documents;
final sum = documents.fold(0, (s, n) => s + int.parse(n['Value']));
return Scaffold(
appBar: AppBar(title: Text("Total sum: $sum")),
body: ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
final documentSnapshot = snapshot.data.documents[index];
final value = int.parse(documentSnapshot['Value']);
return Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Document $index: $value'),
);
},
),
);
},
),
);
}
}
最终合集=Firestore.instance.collection('Expenses'); // 如何获取 uid 然后获取 docs???