Skip to content

Commit

Permalink
🐛 (fix) Upload image failed
Browse files Browse the repository at this point in the history
  • Loading branch information
chandni2428 committed Nov 13, 2020
1 parent 0ba093d commit cbef8bc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
6 changes: 4 additions & 2 deletions lib/page/feed/composeTweet/composeTweet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class _ComposeTweetReplyPageState extends State<ComposeTweetPage> {

/// Submit tweet to save in firebase database
void _submitButton() async {
FocusManager.instance.primaryFocus.unfocus();
if (_textEditingController.text == null ||
_textEditingController.text.isEmpty ||
_textEditingController.text.length > 280) {
Expand Down Expand Up @@ -576,11 +577,12 @@ class _UserList extends StatelessWidget {
user: list[index],
onUserSelected: (user) {
textEditingController.text =
Provider.of<ComposeTweetState>(context)
Provider.of<ComposeTweetState>(context, listen: false)
.getDescription(user.userName);
textEditingController.selection = TextSelection.collapsed(
offset: textEditingController.text.length);
Provider.of<ComposeTweetState>(context).onUserSelected();
Provider.of<ComposeTweetState>(context, listen: false)
.onUserSelected();
},
);
},
Expand Down
33 changes: 8 additions & 25 deletions lib/state/feedState.dart
Original file line number Diff line number Diff line change
Expand Up @@ -389,27 +389,15 @@ class FeedState extends AppState {
notifyListeners();
var storageReference = FirebaseStorage.instance
.ref()
.child('tweetImage${Path.basename(file.path)}');
.child("tweetImage")
.child(Path.basename(file.path));
await storageReference.putFile(file);

storageReference.getDownloadURL().then((fileURL) async {
var url = await storageReference.getDownloadURL();
if (url != null) {
return url;
}
return null;
});
// StorageReference storageReference = FirebaseStorage.instance
// .ref()
// .child('tweetImage${Path.basename(file.path)}');
// StorageUploadTask uploadTask = storageReference.putFile(file);
// var snapshot = await uploadTask.onComplete;
// if (snapshot != null) {
// var url = await storageReference.getDownloadURL();
// if (url != null) {
// return url;
// }
// }
var url = await storageReference.getDownloadURL();
if (url != null) {
return url;
}
return null;
} catch (error) {
cprint(error, errorIn: 'uploadFile');
return null;
Expand All @@ -419,14 +407,9 @@ class FeedState extends AppState {
/// [Delete file] from firebase storage
Future<void> deleteFile(String url, String baseUrl) async {
try {
String filePath = url.replaceAll(
new RegExp(
r'https://firebasestorage.googleapis.com/v0/b/twitter-clone-4fce9.appspot.com/o/'),
'');
String filePath = url.split(".com/o/")[1];
filePath = filePath.replaceAll(new RegExp(r'%2F'), '/');
filePath = filePath.replaceAll(new RegExp(r'(\?alt).*'), '');
// filePath = filePath.replaceAll('tweetImage/', '');
// cprint('[Path]'+filePath);
var storageReference = FirebaseStorage.instance.ref();
await storageReference.child(filePath).delete().catchError((val) {
cprint('[Error]' + val);
Expand Down

0 comments on commit cbef8bc

Please sign in to comment.