我目前正在创建一个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();
}
}
1条答案
按热度按时间3ks5zfa01#
问题是您正在从firebase获取
snapshots
,但是您没有使用其结果的值,而是使用widget.['date']
设置状态其次,不希望取
initState
中的文档,而是使用StreamBuilder