Flutter null satety

하스레·2022년 7월 20일

다음과 같은 에러가 나타났다.

The property 'width' can't be unconditionally accessed because the receiver can be 'null'.
Try making the access conditional (using '?.') or adding a null check to the target 

해결 방법

  • bang operator 사용
int? count = 1;

void main() {
  // will throw an error if count is null
  print(count! * 2);
}
  • ?? operator 사용
int? count = 1;

void main() {
  // safe
  print((count ?? 1) * 2);
}
  • if/else 구문 사용
int? count = 1;

void main() {
  if(count != null) {
    print(count! * 2);
  } else {
    print('count is null');
  }
}

https://stackoverflow.com/questions/66986024/the-operator-cant-be-unconditionally-invoked-because-the-receiver-can-be-n

profile
Software Developer

0개의 댓글