简体   繁体   中英

upload files to firebase storage not working

when I try to upload image to firebase storage using flutter, it doesn't work. It does not give error, but not load to the android divice. It just show running gradle task 'assemble task'.. it does not go from there

在此处输入图像描述

here is my codes which i used to upload images noticeUpload.dart

import 'dart:io';
import 'dart:async';
import 'package:image_picker/image_picker.dart';
import 'package:flutter/material.dart';
import 'package:firebase_storage/firebase_storage.dart';

class NoticeUpload extends StatefulWidget {
    @override
_NoticeUploadState createState() => _NoticeUploadState();
 }

class _NoticeUploadState extends State<NoticeUpload> {
  File sampleImage;
  Future getImage()async{
var tempImage= await ImagePicker.pickImage(source: ImageSource.gallery );
setState(() {
  sampleImage=tempImage;
});

}


  @override
  Widget build(BuildContext context) {
return new Scaffold(
  appBar: AppBar(
    title: Text('Upload Notices'),


  ),
  body: Row(

    children:<Widget>[
      sampleImage==null? Text('select image'):enableUpload(),
      RaisedButton(
        onPressed: getImage,
        child: Text('uppload image'),
        ),
      RaisedButton(
        onPressed: (){

        },
        child:Text('uppload file') ,
      )
    ]
  ),

);
 }
Widget enableUpload(){
   return Container(
  child: Column(
    children: <Widget>[
      Image.file(sampleImage,height:200.0,width:100.0),
      RaisedButton(
        child:Text('Upload') ,
        onPressed: (){
          final StorageReference firebaseStorageRef=
          FirebaseStorage.instance.ref().child('myimage.jpg');
          final StorageUploadTask task=firebaseStorageRef.putFile(sampleImage);
        },
        )
    ],
    ),
);
}
}

this is the build.gradle file.

buildscript {
ext.kotlin_version = '1.3.50'
repositories {
    google()
    jcenter()
}

dependencies {
    classpath 'com.android.tools.build:gradle:3.5.0'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath 'com.google.gms:google-services:4.0.1'
}
}

allprojects {
repositories {
    google()
    jcenter()
}
}

rootProject.buildDir = '../build'
   subprojects {
      project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
     project.evaluationDependsOn(':app')
 }

task clean(type: Delete) {
     delete rootProject.buildDir
 }

subprojects{
     project.configurations.all{
          resolutionStrategy.eachDependency{ details ->
              if(details.requested.group=='com.android.support'
             && !details.requested.name.contains('multidex')
            ){
            details.useVersion "28.0.0"
            }

        }

     }
   }
final ref = FirebaseStorage.instance
          .ref()
          .child('user_image')
          .child(_authResult.user.uid + '.jpg');
      await ref.putFile(file).onComplete;
      final url = await ref.getDownloadURL();
      print("Image URL: " + url);

Follow above code and check you are getting URL or not, if you configured firebase properly in your project then it should work;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM