为什么在给定场景中变量值没有从 null 更新到最新值?
Why is the variable value not updating from null to the latest value in the given scenario?
我正在开发一个项目,我想将文件上传到 firebase 存储并将 url 添加到 firestore。我能够将变量 urlDownload
的更新值打印到控制台,但无法将其发送到 firestore,而是 firestore 中的相应字段显示空值。我知道我基本上做错了什么,请帮我弄清楚。
下面是link代码。主要观察11,94,128行提到的变量urlDownload
。
https://pastebin.com/8HWXgS9b
.
.
class _MainPageState extends State<MainPage> {
UploadTask? task;
File? file;
String? urlDownload;
List<String> types = [
.
.
onPressed: () async {
await uploadFile();
await addFile(
_namecontroller.text,
dropdownValue,
urlDownload,
);
},
.
.
Future uploadFile() async {
.
.
final urlDownload = await snapshot.ref.getDownloadURL();
setState(() {});
print('Download-Link: $urlDownload');
}
您拼错了全局变量(urlDownlaod
对比 urlDownload
)。您还将下载 url 分配给 uploadFile 函数中的局部变量。尝试更正名称并删除第 128 行的 final
关键字。
提示:您可以 return 从 uploadFile 下载 url 而不是将其设为 class 宽变量,因为您不会在其他任何地方使用它。在我看来,它稍微降低了复杂性。
我正在开发一个项目,我想将文件上传到 firebase 存储并将 url 添加到 firestore。我能够将变量 urlDownload
的更新值打印到控制台,但无法将其发送到 firestore,而是 firestore 中的相应字段显示空值。我知道我基本上做错了什么,请帮我弄清楚。
下面是link代码。主要观察11,94,128行提到的变量urlDownload
。
https://pastebin.com/8HWXgS9b
.
.
class _MainPageState extends State<MainPage> {
UploadTask? task;
File? file;
String? urlDownload;
List<String> types = [
.
.
onPressed: () async {
await uploadFile();
await addFile(
_namecontroller.text,
dropdownValue,
urlDownload,
);
},
.
.
Future uploadFile() async {
.
.
final urlDownload = await snapshot.ref.getDownloadURL();
setState(() {});
print('Download-Link: $urlDownload');
}
您拼错了全局变量(urlDownlaod
对比 urlDownload
)。您还将下载 url 分配给 uploadFile 函数中的局部变量。尝试更正名称并删除第 128 行的 final
关键字。
提示:您可以 return 从 uploadFile 下载 url 而不是将其设为 class 宽变量,因为您不会在其他任何地方使用它。在我看来,它稍微降低了复杂性。