在抖动中使用图像列表进行多上传图像

时间:2019-01-28 03:59:53

标签: android firebase dart flutter firebase-storage

我是新手。我尝试使应用程序使用抖动上传一些图像。我已经成功从图像选择器中选择了一些图像,但是当我尝试将数据写入Firebase存储时,仅成功上传了一张图像。这是我的代码:

List<File> _imageList = [];
File _image;

  Future uploadFile() async {
    final FirebaseUser user = await FirebaseAuth.instance.currentUser();
    final userId = user.uid;
    final StorageReference firebaseStorageRef =
        FirebaseStorage.instance.ref().child(userId).child('images');
    final StorageUploadTask task = firebaseStorageRef.putFile(_image);
    return task;
  }

这是我用于选择和显示图像的代码

 Future getImageFromGallery() async {
    var image = await ImagePicker.pickImage(source: ImageSource.gallery);
    setState(() {
      _image = image;
      _imageList.add(_image);
    });
  }

List<Widget> builtImageDisplay() {
    return [
      Padding(
        padding: const EdgeInsets.all(8.0),
        child: new Center(
          child: Padding(
            padding: const EdgeInsets.all(15.0),
            child: _imageList.length == 0
                ? new Image.asset('assets/images/format.jpg')
                : GridView.count(
                    shrinkWrap: true,
                    primary: false,
                    crossAxisCount: 4,
                    mainAxisSpacing: 5.0,
                    crossAxisSpacing: 5.0,
                    children: _imageList.map((File file) {
                      return GestureDetector(
                        onTap: () {},
                        child: new GridTile(
                          child: new Image.file(
                            file,
                            fit: BoxFit.cover,
                          ),
                        ),
                      );
                    }).toList(),
                  ),
          ),
        ),
      )
    ];
  }

1 个答案:

答案 0 :(得分:1)

简单的function即可上传多个图像文件。

import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';

  Future<Null> _uploadImages() async {
    _imageList.forEach((f) {
      final StorageReference _ref = storageImageRef.child(basename(f.path));
      final StorageUploadTask uploadTask = _ref.putFile(f);
    });