Search code examples
flutternavigationuiimagepickercontrollerflutter-navigationflutter-android

Flutter - The argument type 'Null' can't be assigned to the parameter type 'Function'


I was trying to connect my existing screens with the image picker plugin. But for some reason, I am getting the error showing "The argument type 'Null' can't be assigned to the parameter type 'Function'." The code of the button where I used the onSelectVideo and null -

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

Now the thing is I know I don't need to use the null here. But I can't figure out what to use instead of null. The starting code of the next screen is like this -

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);
  }

When I remove the null it shows I need an identifier but I can't make it work. As I am a novice learner in the flutter, I'll appreciate it if you help me solve this issue.


Solution

  • Change null on onSelectedVideo like this onSelectedVideo:(savedImage){}

    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
                                  },
                                ),
                        ),
                      );
                    },
                  ),