【问题标题】:If I did not pick any image it will have an error message如果我没有选择任何图像,则会出现错误消息
【发布时间】:2022-10-12 23:44:57
【问题描述】:

我想使用flutter将图像上传到firebase存储中,但是当我没有选择任何图像时会出错。

当我输入将上传图像的代码时,错误开始发生。如果我还删除了空检查,它还会显示一条错误消息,表明它需要进行空检查。

        import 'package:file_picker/file_picker.dart';
    import 'package:flutter/material.dart';
    import 'package:project/storage_service.dart';

    class UserProfile extends StatefulWidget {
      const UserProfile({super.key});

      @override
      State<UserProfile> createState() => _UserProfileState();
    }

    class _UserProfileState extends State<UserProfile> {
      @override
      Widget build(BuildContext context) {
        final Storage storage = Storage();
        return Scaffold(
          appBar: AppBar(
            title: Text('Profile Picture'),
          ),
          body: Column(
            children: [
              Center(
                  child: ElevatedButton(
                      onPressed: () async {
                        final results = await FilePicker.platform.pickFiles(
                          allowMultiple: false,
                          type: FileType.custom,
                          allowedExtensions: ['png', 'jpg'],
                        );

                        if (results == null) {
                          ScaffoldMessenger.of(context).showSnackBar(
                              const SnackBar(content: Text('No selected file.')));
                        }

                        final path = results?.files.single.path;
                        final fileName = results?.files.single.name;

                        // when I added this line of code there will be an error when I did not choose any image
                        storage
                            .uploadFile(path!, fileName!)
                            .then((value) => print('done'));
                      },
                      child: Text('Upload profile picture')))
            ],
          ),
        );
      }
    }

【问题讨论】:

  • 你能提供实际的错误信息吗?

标签: flutter firebase firebase-storage


【解决方案1】:
if (results == null) {
                  ScaffoldMessenger.of(context).showSnackBar(
                      const SnackBar(content: Text('No selected file.')));
                } else {
                  final path = results.files.single.path;
                  final fileName = results.files.single.name;

                  print(path);
                  print(fileName);
                  storage
                      .uploadFile(path!, fileName)
                      .then((value) => print('done'));
                }
              },

【讨论】:

    【解决方案2】:

    由于问题中当前缺少错误消息,我只能猜测它与此函数调用中的强制可选展开(!)有关

    storage
        .uploadFile(path!, fileName!)
        .then((value) => print('done'));
    

    您应该在访问它们之前检查这些值是否为 null。

    例如通过

    if (path != null && fileName != null) {
        storage
            .uploadFile(path!, fileName!)
            .then((value) => print('done'));
    } else {
        print("path or fileName does not exist") // Potentially because none was selected...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-13
      • 1970-01-01
      相关资源
      最近更新 更多