Home > Software engineering >  Flutter : Dart global variable is output as null
Flutter : Dart global variable is output as null

Time:11-14

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);
  }

CodePudding user response:

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);

CodePudding user response:

Looks like the global one never used. The startScreenRecord generates its own Date.

The stopScreenRecord has no access to the class variable.

If i read the code right. Your functions are outside of the Class. So they have no access to the Class Variables.

  • Related