Error: The argument type 'String?' can't be assigned to the parameter type 'String'
Error: The argument type 'String?' can't be assigned to the parameter type 'String'
我正在开发 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);
}
}
}
The Dart programming language supports null safety。这意味着在 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。
使用!将字符串转换为不可空类型的运算符
File(_file.path!)
String?
表示它可能有值,也可能有null
值,但是String
表示它有一个合适的String类型的值。
您可以将 !
添加到数据类型的最后一个,以确保它不是 null
.
例如:
String? name = "Jack";
String name2 = name!;
我正在开发 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);
}
}
}
The Dart programming language supports null safety。这意味着在 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。
使用!将字符串转换为不可空类型的运算符
File(_file.path!)
String?
表示它可能有值,也可能有null
值,但是String
表示它有一个合适的String类型的值。
您可以将 !
添加到数据类型的最后一个,以确保它不是 null
.
例如:
String? name = "Jack";
String name2 = name!;