Android Studio 请帮助我如何使用iniState从firestore获取数据以获取小部件

jvlzgdj9  于 2023-03-03  发布在  Android
关注(0)|答案(1)|浏览(103)

我目前正在创建一个pdf报告,在那里我可以得到所有的数据收集和它的领域,能够出现在pdf文件使用小部件,现在我似乎无法弄清楚如何使它出现,我尝试使用这种方法。请帮助我我仍然是新的Flutter和消防基地。
所以我尝试了这个方法从firestore获取数据
这是我的全部代码

import 'dart:typed_data';
    import 'package:cloud_firestore/cloud_firestore.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    import 'package:pdf/pdf.dart';
    import 'package:pdf/widgets.dart' as pw;
    import 'package:printing/printing.dart';
    DateTime today = DateTime.now();
    String dateStr = "${today.month}-${today.year}";
    
    class testing extends StatefulWidget {
    
    
    
    
      final _formkey = GlobalKey<FormState>();
      final String date;
    testing({
        required this.date
    });
    
    
    
    
    
      @override
      State<testing> createState() => _testingState();
    }
    
    
    
    
    
    class _testingState extends State<testing> {
      final CollectionReference collectionUser = FirebaseFirestore.instance.collection('ratings');
      final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
    
    
    
    
       String? date;
    
      void initState(){
        super.initState();
        FirebaseFirestore.instance
            .collection("ratings")
            .doc(dateStr)
            .collection('failed rating')
            .snapshots();
        date = widget.date;
    
    
      }
    
      @override
      Widget build(BuildContext context) {
        return  PdfPreview(
           canChangeOrientation: false,
          canDebug: false,
          build:(format) => generateDocument(format,),
        );
      }
    
    Future<Uint8List>generateDocument(PdfPageFormat format) async{
      final doc = pw.Document(pageMode: PdfPageMode.outlines);
      final font1 = await PdfGoogleFonts.openSansRegular();
      final font2 = await PdfGoogleFonts.openSansBold();
    
    
      doc.addPage(
        pw.Page(
          pageTheme: pw.PageTheme(
            pageFormat: format.copyWith(
              marginBottom: 0,
              marginLeft: 0,
              marginRight: 0,
              marginTop: 0,
            ),
            orientation: pw.PageOrientation.portrait,
            theme: pw.ThemeData.withFont(
              base: font1,
              bold: font2,
            ),
          ),
          build: (context){
            return pw.Center(
              child: pw.Column(
                mainAxisAlignment: pw.MainAxisAlignment.center,
                children: [
    
                  pw.Center(
                    child: pw.Text(
                      date!,
                    )
                  )
                ]
              )
            );
          }
        )
      );
      return doc.save();
    }
    
    
    }
3ks5zfa0

3ks5zfa01#

问题是您正在从firebase获取snapshots,但是您没有使用其结果的值,而是使用widget.['date']设置状态
其次,不希望取initState中的文档,而是使用StreamBuilder

相关问题