In this article, we will explore the steps to visualize Flutter DataGrid hosted on Google Drive without the hassle of downloading and opening files on your local machine.
Initialize the SfDataGrid widget with all the required properties. The exportToPdfDocument method is invoked on the current state of the key to export the data from the SfDataGrid as a PdfDocument. A URL for the PDF is created using Blob and createObjectUrl, where the PDF bytes are passed along with the MIME type of application/pdf. The open method is utilized to open the URL in a new browser tab.
final GlobalKey<SfDataGridState> _key = GlobalKey<SfDataGridState>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('SfDatagrid Demo')),
body: Column(children: [
const Padding(padding: EdgeInsets.only(top: 30)),
SizedBox(
child: ElevatedButton.icon(
onPressed: () async {
final PdfDocument document =
_key.currentState!.exportToPdfDocument();
final List<int> data = document.saveSync();
Uint8List bytes = Uint8List.fromList(data);
final url = html.Url.createObjectUrl(
html.Blob([bytes], 'application/pdf'));
html.window.open(url, '_blank');
document.dispose();
},
icon: const Icon(Icons.print),
label: const Text('Export DataGrid to PDF'),
)),
Expanded(
child: Card(
margin: const EdgeInsets.all(30),
child: SfDataGrid(
source: _employeeDataSource,
key: _key,
columns: getColumns,
columnWidthMode: ColumnWidthMode.fill),
))
]));
}