StorageReference.putFile在Flutter中返回NoSuchMethod错误

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

我创建了一个Uploader小部件以用于我的FlutterFire应用。我还在Firebase控制台中初始化了Firebase存储并添加了相关性。但是,当我尝试使用Uploader上传图像文件时,StorageReference对象返回NoSuchMethod错误。检查堆栈跟踪和下面的代码,将对您有所帮助。注意:以上陈述是我的假设,由于我是Flutter社区的新手,请纠正我:)

这里是上传器小部件

class Uploader extends StatefulWidget {
  Future<File> file;

  Uploader({Key key, this.file}) : super(key: key);

  createState() => _UploaderState();
}

class _UploaderState extends State<Uploader> {
  File file;
  final FirebaseStorage _storage =
      FirebaseStorage(storageBucket: 'gs://search-n-found.appspot.com');

  StorageUploadTask _uploadTask;
  _startUpload() {
    String filePath = 'images/test.jpg';

    setState(() {
      _uploadTask = _storage.ref().child(filePath).putFile(file);
    });
  }

  @override
  Widget build(BuildContext context) {
    if (_uploadTask != null && file != null) {
      return StreamBuilder<StorageTaskEvent>(
          stream: _uploadTask.events,
          builder: (_, snapshot) {
            var event = snapshot?.data?.snapshot;

            double progressPercent = event != null
                ? event.bytesTransferred / event.totalByteCount
                : 0;

            return Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  if (_uploadTask.isComplete)
                    Icon(
                      Icons.cloud_done,
                      color: Colors.blue,
                    ),
                  if (_uploadTask.isInProgress)
                    CircularProgressIndicator(
                      value: progressPercent,
                      backgroundColor: Colors.blue,
                    ),
                ]);
          });
    } else {
      return FlatButton.icon(
        color: Colors.blue,
        label: Text('Upload'),
        icon: Icon(Icons.cloud_upload),
        onPressed: _startUpload,
      );
    }
  }
}

我正在从此页面传递图像文件(未来文件类型)

class UploadImages extends StatefulWidget {
  final User user;

  const UploadImages({Key key, this.user}) : super(key: key);
  @override
  _UploadImagesState createState() {
    return _UploadImagesState();
  }
}

class _UploadImagesState extends State<UploadImages> {
  List<Object> images = List<Object>();
  Future<File> _imageFile;
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  void initState() {
    super.initState();
    setState(() {
      images.add("Add Image");
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(
        title: Text(
          'Upload Images Here',
          style: Theme.of(context)
              .textTheme
              .headline5
              .copyWith(color: Colors.amber),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.push(context,
              MaterialPageRoute(builder: (context) => ItemTags(widget.user)));
        },
        child: Text(
          'Next',
          style: TextStyle(color: Colors.amber),
        ),
        backgroundColor: Colors.black,
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            child: buildGridView(),
          ),
          Uploader(file: _imageFile,),//Passing the Future File here
        ],
      ),
    );
  }
}

当引发异常时,这是堆栈


════════ Exception caught by gesture ═══════════════════════════════════════════
The following NoSuchMethodError was thrown while handling a gesture:
The method 'existsSync' was called on null.
Receiver: null
Tried calling: existsSync()

When the exception was thrown, this was the stack
#0      Object.noSuchMethod  (dart:core-patch/object_patch.dart:53:5)
#1      StorageReference.putFile 
package:firebase_storage/src/storage_reference.dart:62
#2      _UploaderState._startUpload.<anonymous closure> 
package:project_search_found/Auth/uploader.dart:24
#3      State.setState 
package:flutter/…/widgets/framework.dart:1233
#4      _UploaderState._startUpload 
package:project_search_found/Auth/uploader.dart:23
...
Handler: "onTap"
Recognizer: TapGestureRecognizer#90ea8
    debugOwner: GestureDetector
    state: possible
    won arena
    finalPosition: Offset(164.5, 663.0)
    finalLocalPosition: Offset(40.0, 29.0)
    button: 1
    sent tap down

这是我的pubspec.yaml

dependencies:
  flutter:
    sdk: flutter
  google_fonts: ^0.4.0
  firebase_auth: ^0.16.0
  cloud_firestore: ^0.13.5
  provider: ^4.0.5+1
  image_picker: ^0.6.6+1
  firebase_storage: ^3.1.5

感谢您的帮助。谢谢:)

firebase flutter firebase-storage
1个回答
0
投票

[嘿Tushar,我想即使您正在使用发布的消防车,但主要问题是他没有包括上装者方法,并且在未提供源代码的情况下使用了它。只要链接可用,我就会上传该链接

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