我正在使用 ChatApp 嘗試保存和上傳影像,但出現這樣的錯誤 有誰知道這是什么原因?我遇到了這些型別的錯誤,我找不到任何解決方案。
import 'dart:io';
//Packages
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:file_picker/file_picker.dart';
const String USER_COLLECTION = "Users";
class CloudStorageService {
final FirebaseStorage _storage = FirebaseStorage.instance;
CloudStorageService();
Future<String?> saveUserImageToStorage(
String _uid, PlatformFile _file) async {
try {
Reference _ref =
_storage.ref().child('images/users/$_uid/profile.${_file.extension}');
UploadTask _task = _ref.putFile(
[enter image description here][1]File(_file.path),
);
return await _task.then(
(_result) => _result.ref.getDownloadURL(),
);
} catch (e) {
print(e);
}
}
Future<String?> saveChatImageToStorage(
String _chatID, String _userID, PlatformFile _file) async {
try {
Reference _ref = _storage.ref().child(
'images/chats/$_chatID/${_userID}_${Timestamp.now().millisecondsSinceEpoch}.${_file.extension}');
UploadTask _task = _ref.putFile(
File(_file.path), ----------------> Here is the error
);
return await _task.then(
(_result) => _result.ref.getDownloadURL(),
);
} catch (e) {
print(e);
}
}
}
uj5u.com熱心網友回復:
使用 !將字串轉換為不可為空型別的運算子
File(_file.path!)
uj5u.com熱心網友回復:
String?意味著它可能有值,或者它可能有null值,但String意味著它有一個合適的值 String 型別。您可以添加!到最后一個資料型別以確保它不是null.
例如:
String? name = "Jack";
String name2 = name!;
uj5u.com熱心網友回復:
Dart 編程語言支持空安全。這意味著在 Dart 中可空型別和不可空型別是完全不同的。例如
bool b; // can be true or false
bool? nb; // can be true, false or null, `?` is explicit declaration, that type is nullable
所以,String?和String是完全不同的型別。第一個可以是字串或空值,第二個只能是一個字串。并且您需要在您的情況下檢查 null 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/351472.html
標籤:火力基地 扑 Firebase 实时数据库 谷歌云firestore 谷歌云功能
