【问题标题】:Flutter - The argument type 'Null' can't be assigned to the parameter type 'Function'Flutter - 参数类型'Null'不能分配给参数类型'Function'
【发布时间】:2021-12-22 15:40:55
【问题描述】:

我试图将我现有的屏幕与图像选择器插件连接起来。但由于某种原因,我收到错误消息,显示“无法将参数类型‘Null’分配给参数类型‘函数’。” 我使用 onSelectVideo 和 null 的按钮的代码 -

ElevatedButton(
                child: Text(
                  'Next',
                  style: TextStyle(fontSize: 17, color: Colors.white),
                ),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => video_record02(
                              onSelectVideo: null,
                            ),
                    ),
                  );
                },
              ),

现在我知道我不需要在这里使用 null。但我不知道用什么代替null。 下一屏的起始代码是这样的-

class video_record02 extends StatefulWidget {
  final Function onSelectVideo;

  const video_record02({Key? key, required this.onSelectVideo})
      : super(key: key);

  @override
  _video_record02State createState() => _video_record02State();
}

class _video_record02State extends State<video_record02> {
  File? storedImage;

  Future<void> takeVideo() async {
    final picker = ImagePicker();
    final videoFile = await picker.pickVideo(
      source: ImageSource.camera,
      preferredCameraDevice: CameraDevice.rear,
      maxDuration: Duration(
        seconds: 25,
      ),
    );
    if (videoFile == null) {
      return;
    }
    final rlyvideoFile = File(videoFile.path);
    setState(() {
      storedImage = rlyvideoFile;
    });
    final appDir = await syspaths.getApplicationDocumentsDirectory();
    final fileName = path.basename(rlyvideoFile.path);
    final savedImage = await rlyvideoFile.copy('${appDir.path}/$fileName');
    widget.onSelectVideo(savedImage);
  }

当我删除 null 时,它显示我需要一个标识符,但我无法让它工作。由于我是一个新手学习者,如果你能帮助我解决这个问题,我将不胜感激。

【问题讨论】:

    标签: flutter navigation uiimagepickercontroller flutter-navigation flutter-android


    【解决方案1】:

    onSelectedVideo:(savedImage){} 这样在 onSelectedVideo 上更改 null

    ElevatedButton(
                    child: Text(
                      'Next',
                      style: TextStyle(fontSize: 17, color: Colors.white),
                    ),
                    onPressed: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => video_record02(
                                  onSelectVideo: (savedImage){
                                  //fill some code you want to execute if the video was selected
                                  },
                                ),
                        ),
                      );
                    },
                  ),
    

    【讨论】:

    • 谢谢,成功了!错误消失了。但我不知道为什么,当应用程序打开时相机停止工作。在您建议的更改之后,我是否需要在那里添加一些额外的代码?
    猜你喜欢
    • 2021-11-24
    • 2021-06-28
    • 1970-01-01
    • 2022-01-17
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    相关资源
    最近更新 更多