Comparison of Flutter Dart reference methods with and without parentheses

Compare by code

class Person {
  bool say(String msg) {
    print(msg);
    return msg == null ? false : true;
  }

  void setPeople() {
    /**
     *  People.setSay1 参数需要是一个bool值
     *  say("im people")带括号就相当于调用方法拿到返回bool值
     *  所以没问题 
     *  */
    new People.setSay1(isSay: say("im people"));

    /**
     *  People.setSay2 参数是需要 bool Function(String)
     *  say("im people")带括号就是一个bool值
     *  所以会报错
     *  The argument type 'bool' can't be assigned to 
     *  the parameter type 'bool Function(String)'.dartargument_type_not_assignable
     *  */
    new People.setSay2(methodSay: say("im people"));

    /**
     * 直接用 say方法,不带括号不传参数,就代表的了say方法本身
     * say方法 结构就是bool Function(String)
     * 所以没问题
     */
    new People.setSay2(methodSay: say);
  }
}

typedef MethodSay = bool Function(String msg);

class People {
  String peopleSayMsg;
  bool isSay;
  People.setSay1({bool isSay}) {
    this.isSay = isSay;
  }
  People.setSay2({MethodSay methodSay}) {
    methodSay(peopleSayMsg);
  }
}

to sum up:

The use of the method name (parameter) in    this way is equivalent to the value obtained after calling the method. What is the return type of this method is what type of value it is

The method name                is used directly without parentheses. The structure of the method itself is passed, not the return value after the operation.

Guess you like

Origin blog.csdn.net/u011288271/article/details/106100410