如果formattedDate
宣告為全域變數并formattedDate
在startScreenRecord中輸出,則輸出良好。但是,如果formattedDate
在 stopScreenRecord 中再次輸出,則輸出為 null。我該如何解決這個問題?
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);
}
uj5u.com熱心網友回復:
formattedDate
這是因為您的代碼中有兩個同名的不同變數。讓我解釋:
有
var formattedDate;
- 你的類的一個實體變數_HomePageState
,可以通過this.formattedDate
.有
startScreenRecord(bool audio, String formattedDate)
帶有引數的函式。所以在函式體內,有formattedDate
和this.formattedDate
,但它們是不同的!
當你在寫作
var formatter = new DateFormat('yyyy_MM_dd_HH_mm_ss');
formattedDate = formatter.format(now);
在函式體內,您實際上是在修改作為引數傳遞給函式的變數。您沒有修改實體變數。你需要做的是
var formatter = new DateFormat('yyyy_MM_dd_HH_mm_ss');
this.formattedDate = formatter.format(now);
uj5u.com熱心網友回復:
看起來像全域的從未使用過。startScreenRecord
生成自己的日期。
stopScreenRecord
無權訪問類變數。
如果我正確閱讀了代碼。您的功能在類之外。所以他們無法訪問類變數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/533174.html
標籤:扑镖全局变量
上一篇:將型別存盤在變數中時Darttype_test_with_non_type
下一篇:在null上呼叫了getter'length'。相關的導致錯誤的小部件是StreamBuilder<List<DocumentSnapshot<Object?&g