我正在try 从FiRestore加载数据,在StreamBuilder的帮助下,我正在获取相应的数据并将其显示给用户.

我将这个值传递给一个名为‘Total’的变量,这个合计值被转换为0,当我试图显示这个合计值时,它首先应该是缺省的(0)值,它从FiRestore中获取值.

下面您可以看到代码

            StreamBuilder<DocumentSnapshot>(
                stream: FirebaseFirestore.instance.collection('UserData').doc(getUid()).snapshots(),
                builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
                  if (!snapshot.hasData) return const Text("Loading...");
                  final DocumentSnapshot? document = snapshot.data;
                  final Map<String, dynamic> documentData = document?.data() as Map<String, dynamic>;

                  accountList = (documentData['accounts'] as List).map((accountList) => accountList as Map<String, dynamic>).toList();
                  // getTotalFund(accountList);
                  List amountData=[];
                  double convertAmount;

                  for(int i=0; i<accountList.length;i++){
                    if(accountList[i]['accountType'] == 'Savings' || accountList[i]['accountType'] == 'Cash' ){
                      amountData.add(double.parse(accountList[i]['amountBalance'].replaceAll(',','')));
                    }
                  }
                  convertAmount = amountData.reduce((value, element) => value + element);
                  totalFund = amountFormat.format(convertAmount);

                  return Text(
                    documentData['name'],
                    style: const TextStyle(
                      fontSize: 16,
                      fontFamily: "Gilroy SemiBold",
                    ),
                  );
                }),

我在文本小部件中获得的值总金额.

Text('\u{20B9} ', style: TextStyle(color: Color(0xff9c9c9c), fontFamily: "Gilroy Medium", fontSize: 23)),
Text(totalFund.toString(),style: const TextStyle(color: Colors.black, fontFamily: "Gilroy Bold", fontSize: 35)),

如何在initState中加载totalFund,使其不会显示空值.

screenshot

推荐答案

试试这个:

 Future<DocumentSnapshot> getDoc() async {
    return await FirebaseFirestore.instance.collection('UserData').doc(getUid());
 }

然后将FutureBuilder设置为Body小部件的父部件,如下所示:

FutureBuilder(future: getDoc(),
       builder: (context, snapshot){
            if (snapshot.connectionState == ConnectionState.waiting) {
                  return const Text("Loading...");
                } else {
                  if (snapshot.hasError) {
                    return const Text("error");
                  } else {
                    final DocumentSnapshot? document = snapshot.data;
                    final Map<String, dynamic> documentData =
                        document?.data() as Map<String, dynamic>;

                    accountList = (documentData['accounts'] as List)
                        .map((accountList) => accountList as Map<String, dynamic>)
                        .toList();
                    // getTotalFund(accountList);
                    List amountData = [];
                    double convertAmount;

                    for (int i = 0; i < accountList.length; i++) {
                      if (accountList[i]['accountType'] == 'Savings' ||
                          accountList[i]['accountType'] == 'Cash') {
                        amountData.add(double.parse(
                            accountList[i]['amountBalance'].replaceAll(',', '')));
                      }
                    }
                    convertAmount =
                        amountData.reduce((value, element) => value + element);
                    totalFund = amountFormat.format(convertAmount);

                    return yourBodyWidget;
                  }
          })

Flutter相关问答推荐

Flutter 导致底部溢出

Flutter:InteractiveViewer中可伸缩SVG背景上的定位小部件不对齐

Flutter -使用最小高度展开

想更新ListView上的索引点击块Flutter

使用ThemeProvider不在亮模式和暗模式之间切换

来自FutureProvider的数据只打印两个不同的变量,它们对一个实例只有一次相同的值,为什么?

Flutter -如何将导航轨道标签靠左对齐?

Flutter BLoC et workmanager:如何创建独特的仓库初始化模式?

如何在用Ffltter成功登录后重定向下一页?

如何在表格日历标题下方添加文本?

blocTest错误:没有从`when()`中调用方法存根

为什么Spotify Auth API返回400,而";code_verier不正确?

框中的Flutter 适配图像

Wired Firebase错误-[CLOUD_FIRESTORE/UNAvailable]该服务当前不可用

找不到任何与 com.google.firebase:firebase-sessions:[15.0.0, 16.0.0) 匹配的版本

Flutter:使用 GlobalKey 访问 BlocProvider

在使用 Android 12+ 的真实 Android 设备上,SingleChildScrollView 中最初位于视口之外的无响应 Web 视图

什么最接近 Dart 的 List.asMap().forEach() 返回列表?

为什么我们使用 RiverpodGenerator

更新未知文档 ID firebase\flutter 中的字段