gaurangkeluskar22 / Basic-Music-Player-App

A basic music player app, where you can upload your favourite songs and enjoy songs peacefully.
13 stars 7 forks source link

For some reason, even though I have all the code, it is still returning file uploaded unsuccessfully. #3

Open elompo23 opened 3 years ago

elompo23 commented 3 years ago

Here is my code (I added some code for my app):

import 'dart:io'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:file_picker/file_picker.dart'; import 'package:firebase_storage/firebase_storage.dart'; import 'package:flutter/material.dart'; import 'package:CampFire/pages/home.dart'; import 'widget.dart'; import 'package:path/path.dart';

class Upload extends StatefulWidget { @override _UploadState createState() => _UploadState(); }

class _UploadState extends State { TextEditingController songname = TextEditingController(); TextEditingController artistname = TextEditingController();

File image, song; String imagepath, songpath; Reference ref; var image_down_url; var song_down_url; final firestoreinstance = FirebaseFirestore.instance;

void selectimage() async { FilePickerResult result = await FilePicker.platform.pickFiles();

setState(() {
  image = image;
  imagepath = basename(image.path);
  uploadimagefile(image.readAsBytesSync(), imagepath);
});

}

Future uploadimagefile(List image, String imagepath) async { ref = FirebaseStorage.instance.ref().child(imagepath); UploadTask uploadTask = ref.putData(image);

return image_down_url = await (await uploadTask).ref.getDownloadURL(); }

void selectsong() async { FilePickerResult result = await FilePicker.platform.pickFiles();

setState(() {
  song = song;
  songpath = basename(song.path);
  uploadsongfile(song.readAsBytesSync(), songpath);
});

}

Future uploadsongfile(List song, String songpath) async { ref = FirebaseStorage.instance.ref().child(songpath); UploadTask uploadTask = ref.putData(song);

return song_down_url = await (await uploadTask).ref.getDownloadURL(); }

finalupload(context) { if (songname.text != '' && song_down_url != null && image_down_url != null) { print(songname.text); print(artistname.text); print(song_down_url.toString()); print(image_down_url.toString());

  var data = {
    "song_name": songname.text,
    "artist_name": artistname.text,
    "song_url": song_down_url.toString(),
    "image_url": image_down_url.toString(),
  };

  firestoreinstance
      .collection("songs")
      .doc()
      .set(data)
      .whenComplete(() => showDialog(
            context: context,
            builder: (context) =>
                _onTapButton(context, "Files Uploaded Successfully :)"),
          ));
} else {
  showDialog(
    context: context,
    builder: (context) =>
        _onTapButton(context, "Please Enter All Details :("),
  );
}

}

_onTapButton(BuildContext context, data) { return AlertDialog(title: Text(data)); }

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text( "CampFire", style: TextStyle(fontSize: 20), ), actions: [ GestureDetector( onTap: () { Navigator.pushReplacement( context, MaterialPageRoute(builder: (context) => Home())); }, child: Container( padding: EdgeInsets.symmetric(horizontal: 16), child: Icon(Icons.exit_to_app))) ], ), body: Column( children: [ RaisedButton( onPressed: () => selectimage(), child: Text("Select Image"), ), RaisedButton( onPressed: () => selectsong(), child: Text("Select Audio"), ), Padding( padding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0), child: TextField( controller: songname, style: TextStyle(color: Colors.white), decoration: textFieldInputDecoration("Enter a song name"), ), ), Padding( padding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0), child: TextField( controller: artistname, style: TextStyle(color: Colors.white), decoration: textFieldInputDecoration("Enter artist name"), ), ), RaisedButton( onPressed: () => finalupload(context), child: Text("Upload"), ) ], )); } }

elompo23 commented 3 years ago

I think it may be the way I have been uploading the audio and images, anything would help.

gaurangkeluskar22 commented 3 years ago

I think you should upgrade the versions of libraries mentioned in pubspec file, or try to upload the small song file. Hope it works for you.

On Mon, Feb 22, 2021, 9:15 AM elompo23 notifications@github.com wrote:

I think it may be the way I have been uploading the audio and images, anything would help.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/gaurangkeluskar22/Basic-Music-Player-App/issues/3#issuecomment-783055803, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKNI75NFEOKRAWPWVFZODJDTAHHM3ANCNFSM4X7XA5OA .