将文件上传到Firebase存储不起作用

问题描述 投票:0回答:1

当我尝试使用flutter将图像上传到Firebase存储时,它不起作用。它不会给出错误,但不会加载到android设备。它只是显示正在运行的gradle任务“汇编任务” ..并非从那里开始

enter image description here

这是我用来上传图像的代码notifyUpload.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);
        },
        )
    ],
    ),
);
}
}

这是build.gradle文件。

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"
            }

        }

     }
   }
flutter build.gradle firebase-storage
1个回答
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);

按照上述代码检查您是否获取URL,如果您在项目中正确配置了Firebase,则它应该可以工作;

© www.soinside.com 2019 - 2024. All rights reserved.