save method Null safety

void save(
  1. {required String name,
  2. required String subject,
  3. required BuildContext context}
)

save the pdf at /subject/name.pdf

Implementation

static void save({
  required String name,
  required String subject,
  required BuildContext context,
}) async {
  //save the pdf

  /// get the root directory
  final dir = await getApplicationDocumentsDirectory();

  // create a path
  final path = dir.absolute.path + "/$name";

  /// create a file and give it the path
  final File pdfFile = File(path);

  /// save the pdf to the file
  FirebaseStorage.instance
      .ref("pdf")
      .child("grade12")
      .child(subject)
      .child(name)
      .writeToFile(pdfFile)
      .then((p0) {
    ///check if the download is complete
    if (p0.bytesTransferred >= p0.totalBytes) {
      /// save the state
      SharedPrefs.save(
        name: name,
        subject: subject,
      );

      /// log the event
      FirebaseAnalytics.instance.logEvent(
        name: "download",
        parameters: {
          "flie": name,
        },
      );

      /// let the user know the download is complete

      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(
          content: Text("Download complete"),
        ),
      );
    }
  });
}