我正在学习使用新的Dart扩展方法。
我正在这样做:
extension StringInsersion on StringBuffer {
void insertCharCodeAtStart(int codeUnit) {
final end = this.toString();
this.clear();
this.writeCharCode(codeUnit);
this.write(end);
}
int codeUnitAt(int index) {
return this.toString().codeUnitAt(index);
}
}
以便我可以做这样的事情:
myStringBuffer.insertCharCodeAtStart(0x0020);
int value = myStringBuffer.codeUnitAt(2);
但是,我收到以下棉绒警告:
除非避免阴影,否则请不要使用
this
访问成员。
我应该做些不同的事情吗?
您收到的警告表示以下含义:无需使用关键字this
引用当前实例。一切都将在不引用当前实例的情况下起作用,因为静态扩展方法本身充当可扩展类型的实例方法。
简单地说,只需从代码中删除对当前实例的引用。
来自此:
final end = this.toString();
为此:
final end = toString();