Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter : Dart global variable is output as null

If formattedDate is declared as a global variable and formattedDate is output in startScreenRecord, the output is good. However, if formattedDate is output again in stopScreenRecord, it is output as null. how can I solve this?

    class _HomePageState extends State<HomePage> {
  var formattedDate;
  bool recording = false;
  var directory = Directory('/storage/emulated/0/DCIM/BackCamera').create(recursive: true);
  bool inProgress = false;

  Future<File> moveFile(File sourceFile, String newPath) async {
    try {

      return await sourceFile.rename(newPath);
    } on FileSystemException catch (e) {
      final newFile = await sourceFile.copy(newPath);
      await sourceFile.delete();
      return newFile;
    }
  }

  startScreenRecord(bool audio, String formattedDate) async {
    bool start = false;

    var now = new DateTime.now();
    var formatter = new DateFormat('yyyy_MM_dd_HH_mm_ss');
    formattedDate = formatter.format(now);
    print('-------------');
    print(formattedDate);
    print('-------------');
    if (audio) {
      start = await FlutterScreenRecording.startRecordScreenAndAudio(formattedDate);
    } else {
      start = await FlutterScreenRecording.startRecordScreen(formattedDate);
    }
    if (start) {
      setState(() => recording = !recording);
    }
    return start;
  }
  stopScreenRecord() async {
    String path = await FlutterScreenRecording.stopRecordScreen;
    setState(() {
      recording = !recording;
    });
    print("Opening video");
    print(path);
    final directory = await getTemporaryDirectory();
    print(directory.path);
    // Move Title.mp4 to Download folder
    final newPath = '/storage/emulated/0/DCIM/' + "$formattedDate.mp4";
    print(formattedDate);
    print(newPath);
    print(formattedDate.toString());
    final file = await moveFile(File(path), newPath);
    print(file.path);
  }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is because there are two different variables with the same name formattedDate in your code. Let me explain:

  1. There is var formattedDate; – An instance variable of your _HomePageStateclass, can be referred via this.formattedDate.

  2. There is the function startScreenRecord(bool audio, String formattedDate) with its parameters. So inside the function body, there are both formattedDate and this.formattedDate, but they are different!

When you are writing

var formatter = new DateFormat('yyyy_MM_dd_HH_mm_ss');
formattedDate = formatter.format(now);

within the function body, you are actually modifying the variable that was passed to the function as a parameter. You are not modifying the instance variable.
What you instead need to do is

var formatter = new DateFormat('yyyy_MM_dd_HH_mm_ss');
this.formattedDate = formatter.format(now);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading