https://github.com/PuzzleLeaf/flutter_mvvm_tutorial 에서 받은 소스코드가 null-safety 이전 버전이라서 제대로 동작되지 않아서 수정하는 과정을 적어둔 것이다.
Null Safety 를 이용하기 위해서는 pubspec.yaml 파일의 environment 의 버전을 2.12.0 버전 이상이어야 한다.
environment:
sdk: ">=2.12.0 <3.0.0"
Null Safety 지원 확인 및 dependencies: 자동 업데이트
- Teminal 창에서 아래와 같이 입력한다.
dart pub outdated --mode=null-safety
dart pub upgrade --null-safety
|


Flutter Upgrade 방법
// 터미널 창에서 아래 두줄을 입력한다.
flutter channel stable
flutter upgrade
|
AndroidManifest.xml 파일 수정

빨간색 박스 부분을 아래와 같이 수정한다.
android:name="${applicationName}"
그리고 Android Compile 버전을 31로 할 경우에는 android:exported="true" 한줄을 추가해야 한다.

Setting 에서 Plugins 에 업데이트 할 사항이 있다면 Update 해준다.

Android app build.gradle 수정사항

android\gradle\wrapper\gradle-wrapper.properties to gradle-6.5-all.zip

최종적으로 아래와 같이 수정했다.

android gradle 수정

기능 동작 테스트를 거쳐서 최종적으로 아래와 같이 수정했다.

Null safety 적용 전 버전의 모든 변수는 Nullable 이였지만 Flutter 2.0 이상부터는 Nullable 변수를 만들려면 꼭 "?" 를 선언해야 된다.
"Class 내의 변수"는 반드시 선언과 동시에 초기화를 시켜야 한다.
변수 초기화를 위해서 생성자를 만들어주거나 아니면 late keyword를 붙여 줘야 한다.
class Person {
String name;
int age;
Person({required this.name, required this.age});
}
main(){
Person p = Person(name: "홍길동", age: 25);
print(p);
print(p.name);
print(p.age);
}
|
news_article.dart 파일 내용을 아래와 같이 수정했다.
전체 소스코드를 일일이 확인하면서 수정한 것이 아니라 에러가 발생하는 부분을 찾아서 수정하다보니 null 처리를 위해 ? 붙이는 것으로 대체했다.
class NewsArticle {
final String? title;
final String? author;
final String? description;
final String? urlToImage;
final String? url;
late final String publishedAt;
final String? content;
NewsArticle(
{required this.title,
required this.author,
required this.description,
required this.urlToImage,
required this.url,
required this.publishedAt,
required this.content}
);
factory NewsArticle.fromJson(Map<String, dynamic> json) {
return NewsArticle(
title: json['title'],
author: json['author'],
description: json['description'],
urlToImage: json['urlToImage'],
url: json['url'],
publishedAt: json['publishedAt'],
content: json['content'],
);
}
}
|
'Flutter 앱 > 환경설정' 카테고리의 다른 글
Flutter Dart Data Class 자동 생성 (0) | 2023.12.13 |
---|---|
Flutter freezed data class 자동완성 (Android Studio) (0) | 2023.12.08 |
IntelliJ IDEA Community 에 Dart 설치 (0) | 2022.06.24 |
Flutter Upgrade (0) | 2022.06.17 |
dart-sdk 설치 (0) | 2022.01.11 |