在尝试使用noSuchMethod().时得到警告
未为类Person定义缺少的方法。
但是根据docs和其他示例,无论何时我们调用一个不存在的成员,都应该调用noSuchMethod()。其默认行为是抛出noSuchMethodError。
void main() {
var person = new Person();
print(person.missing("20", "Shubham")); // is a missing method!
}
class Person {
@override
noSuchMethod(Invocation msg) => "got ${msg.memberName} "
"with arguments ${msg.positionalArguments}";
} 发布于 2018-12-13 16:11:14
根据用于调用未实现方法的正式文档,必须满足以下几点之一:
示例1: Satifies
class Person {
@override //overring noSuchMethod
noSuchMethod(Invocation invocation) => 'Got the ${invocation.memberName} with arguments ${invocation.positionalArguments}';
}
main(List<String> args) {
dynamic person = new Person(); // person is declared dynamic hence staifies the first point
print(person.missing('20','shubham')); //We are calling an unimplemented method called 'missing'
}示例2: Satifies第二
class Person {
missing(int age,String name);
@override //overriding noSuchMethod
noSuchMethod(Invocation invocation) => 'Got the ${invocation.memberName} with arguments ${invocation.positionalArguments}';
}
main(List<String> args) {
dynamic person = new Person(); //person could be var, Person or dynamic
print(person.missing(20,'shubham')); //calling abstract method
}https://stackoverflow.com/questions/53761294
复制相似问题