简体   繁体   English

上传到 Firebase 存储失败

[英]Uploading to Firebase Storage fail

After running code below and calling method enableUpload() (pressing button), firstly I get an error "Column's children must not containt any null values. It is weird, because there already is an "if" in case file was a null.在运行下面的代码并调用方法 enableUpload()(按下按钮)后,首先我收到一个错误“列的子项不得包含任何 null 值。这很奇怪,因为如果文件是 null,则已经有一个“if”。

And secondly - nothing is uploaded to the storage.其次 - 没有任何东西上传到存储中。

Thanks you in advance.提前谢谢你。

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  static File image;

  Future getImage() async {
    var tempImage = await ImagePicker().getImage(source: ImageSource.gallery);

    setState(() {
      image = File(tempImage.path);
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        theme: ThemeData(brightness: Brightness.dark),
        home: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            image == null ? Text("pick image") : enableUpload(),
            RaisedButton(onPressed: getImage),
          ],
        ));
  }

  Widget enableUpload() {
    if (image == null) return CircularProgressIndicator();
    ListView(
      children: <Widget>[
        Image.file(image),
        RaisedButton(onPressed: () {
          final StorageReference ref =
              FirebaseStorage.instance.ref().child('images');
          ref.putFile(image);
        })
      ],
    );
  }
}

For your first question, you're getting null because the code is missing a return before the ListView .对于您的第一个问题,您将得到null ,因为代码在ListView之前缺少return So you should have:所以你应该有:

Widget enableUpload() {
    if (image == null) return CircularProgressIndicator();

    return ListView(
        //Rest of code
    );
  }

Use return before your ListView .在您的ListView之前使用return

Because Widget enableUpload() {} requires a return因为Widget enableUpload() {}需要return

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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