본문 바로가기
TIL

20230830 TIL : 급할수록 천천히 기초다지기

by 김빵그 2023. 8. 30.

Today 요약

[] 프로그래머스 코딩테스트 lv0 1문제

[] flutter 강의 2개

[] dart 강의 2개


배운 점

1) flutter dart static ? 

  • static은 dart 클래스 내에서 사용되는 한정자로 해당 클래스의 인스턴스 없이 직접 클래스 자체에 접근 할 때  사용
  • 클래스 레벨의 속성 또는 메서드 : static 키워드 사용시 해당 속성 또는 메서드가 클래스 수준에 졵재하게 된다
  • 인스턴스 없이 접근 가능 : 클래스의 인스턴스를 생성지 않고도 static으로 정의된 멤버에 접근 가능하다 
  • 인스턴스 변수에 접근할 수 없음
class MyClass {
  static int staticVariable = 10; // static 변수

  static void staticMethod() { // static 메서드
    print("This is a static method.");
  }

  void instanceMethod() {
    print("This is an instance method.");
  }
}

void main() {
  print(MyClass.staticVariable); // static 변수에 접근
  MyClass.staticMethod(); // static 메서드 호출

  var myInstance = MyClass();
  myInstance.instanceMethod(); // 인스턴스 메서드 호출
}
  • 인스턴스 란?  클래스에 의해 정의된 청사진 또는 템플릿을 기반으로 실제 데이터가 채워진 구체적인 객체를 의미
  • 아래의 코드처럼 클래스를 기반으로 여러개의 자동차 인스턴스를  생성할수 있다 뭐 간편하게 클래스가 설계도, 인스턴스는 그 설계도를 따라 만들어진 실제 물건이라고 보면 될 거 같다 
class Car {
  String make;
  String model;

  void start() {
    print("The car is starting.");
  }
}

void main() {
  Car myCar = Car(); // Car 클래스의 인스턴스 생성
  myCar.make = "Toyota";
  myCar.model = "Corolla";
  
  myCar.start(); // start 메서드 실행
}

2) flutter arguments ? 

  • 함수나 프로그램에 전달되는 값들 인수 또는 매개변수라고도 불린다

3) 화면간 argument 보내기

3-1 main.dart

Widget build(BuildContext context ) {
	return MaterialApp(
    	initialRoute : FirstScreen.routeName,
    ),
    routes :{
    	FirstScreen.routeName : (context) => const FirstScreen(),
        SecondScreen.routeName : (context) => const SecondScreen(),
    }
}

3-2 FirstScreen.dart

Class FirstScreen extends StatelessWidget {
	static String routeName = "/first";
    
    void _onSecondTap() {
    	if(_username.isEmpty) return;
    	Navigator.pushNamed(
        	context, 
            SecondScreen.routeName,
            arguments : SecondScreenArgs (
            	username : _username,
            )
        )
    }
}

3-3 SecondScreen.dart

class SecondScreenArgs{
	final String username; 
    SecondScreenArgs({ required this.username})' // constructor를 만들어주면 끝
}
class SecondScreen extends StatefulWidget {
	static String routeName = "/second";
}

@overrride 
Widget build(BuildContext context ) {
	final args = ModalRoute.of(context)!.settings.arguments as SecondScreenArgs;
    print(args.username);
}
  • SecondScreenArgs : secondScreen 화면으로 데이터를 전달하기 위한 데이터 클래스 생성자를 통해 값을 받아온다 username
  • build 메서드 내에서 ModalRoute.of(context)!.settings.arguments 로 전달된 인수를 가지고 와 username  출력

4)navagitor1을 왜 다른 걸로 교체해야하는지? 복잡한 앱에는추천하지 않는다

5) dart comment

//코멘트

/**
* 줄이 여러개인 코멘트는
* 이렇게 사용 가능하다
*/

/* 코멘트 엔터
줄글~~도가능 */

6) dart start

void main() {

}
void = 리턴값의 타입 
main = 함수 
() = 입력값 
{} 바디
  • 다트 언어 시작되는 부분은 main 함수이며 main이라는 함수가 있어야 실행된다

주절주절

기초도 다시 조금씩 봐야겠다! 기초부터 탄탄히 ..  !

'TIL' 카테고리의 다른 글

20230902 TIL : 중꺾마,, !  (5) 2023.09.02
20230901 TIL : 9월도 뿌시기  (0) 2023.09.01
20230828 TIL : 월요일 좋아 싫어  (0) 2023.08.28
20230826 TIL  (1) 2023.08.26
20230823 TIL  (1) 2023.08.23