I have a method inside a state class which calls setState, however whenever it is called it throws a UnsupportedError (Unsupported operation: read-only)
and gives no other information. Can you see anything wrong with my code that would make it do this? I seems like this should be pretty straightforward...
Future _uploadFile(imageFile, imageFilename, String imageNumber) async {
_user = await DBProvider.db.getUser();
final FirebaseStorage _storage = FirebaseStorage(storageBucket: 'gs://circle-fc687.appspot.com');
StorageReference _storageRef = _storage.ref().child('users').child('${_user['uid']}').child('$imageFilename');
final Directory systemTempDir = Directory.systemTemp;
final File file = await File('${systemTempDir.path}/$imageFile').create();
StorageUploadTask _uploadTask = _storageRef.putFile(file);
await _uploadTask.onComplete;
print('Upload complete');
String downloadLink = await _storageRef.getDownloadURL();
setState(() {
_user['imageOne'] = downloadLink;
});
}
Edit
calling setState
is not the problem, as trying to update a Map property _user['imageOne'] = downloadLink;
from within this method also causes the same error. This variable is not a final or anything like that, just Map<String, dynamic>
Usually DBs return immutable/unmodifiable data, so you have to clone it before changing:
final newUser = {
..._user,
'imageOne': downloadLink
};
or
final newUser = Map.of(_user);
newUser['imageOne'] = downloadLink;
Though an unmodifiable map is inherited from the same Map class, it has a different runtime type that doesn't actually support []= operation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With