Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: The argument type '() → Null' can't be assigned to the parameter type '(Null) → FutureOr<dynamic>'

Tags:

flutter

dart

    import 'package:flutter/material.dart';
    import 'package:camera/camera.dart';


    class Register extends StatefulWidget{
      List<CameraDescription> cameras;
      @override
      _Register createState() {
        return _Register();
      }

      Register(this.cameras);
    }

    class _Register extends State<Register>{

      CameraController controller;

      @override
      Widget build(BuildContext context){
        return MaterialApp(
          title: 'Registration Certificate',
          home: Scaffold(
            appBar: AppBar(
              title: new Text('Register'),
            ),

          body: Container()
          ),
        );
      }


      @override
      void initState(){
        super.initState();
        controller = new CameraController(widget.cameras[0], ResolutionPreset.medium);
        //**SYNTAX ERROR from the below code**
        controller.initialize().then(() {});
      }
    }

ERROR DETAILS

error: The argument type '() → Null' can't be assigned to the parameter type '(Null) → FutureOr'. (argument_type_not_assignable at [fluttercam] lib\packs\reg.certificate.dart:38)

When I use '_' underscore the code is working fine, Error resolved!

controller.initialize().then((_) {});

Can anyone explain what is going on behind the scenes?

like image 534
Rajath Avatar asked Nov 26 '18 06:11

Rajath


1 Answers

When a function that takes a parameter is expected,

(Null) → FutureOr<dynamic>

you can't pass a function that accepts none

() → Null

_ is a valid parameter name and is by convention used to indicate that the parameter is not used and makes the passed function compatible with the defined parameter type because it now accepts a parameter.

controller.initialize() returns a Future and the then(...) method expects a function that it calls when the async task is completed and the result of the completed async task is passed as a parameter to that callback function.

https://api.dartlang.org/stable/2.1.0/dart-async/Future/then.html shows the parameter definition for then:

FutureOr<R> onValue(T value)

which is a function that accepts a parameter of the generic type T and returns a value of FutureOr<R>

like image 57
Günter Zöchbauer Avatar answered Nov 08 '22 13:11

Günter Zöchbauer