4
4

More than 3 years have passed since last update.

プログラミング初心者的にFlutterで分かりづらいことまとめ

Last updated at Posted at 2020-12-05

はじめに

僕はプログラミングで最初に触ったのがFlutterです。
Flutterを始めたのは、2020年3月。
執筆している現在は2020/12月。

初心者向けの記事になるかなと思います。
また、Flutterに限らず、プログラミングの共通の基礎的な内容が含まれます。

用語

用語も「大体こんな感じだろう」と、具体的にどの部分なのかきっちり説明できないことが多い気がする。
しかも、用語の意味が分からないせいでインプットの効率が落ちる。

クラス

クラスはわかりやすい。classって書いてあるので。
クラス名は大文字で始める。

class MyApp{
//中身
}

インスタンス(インスタンス変数)

クラス内で使用する変数。
下の例で言うと、text,countのこと。

class MyApp {
  String text; //ここ
  int count = 1; //ここ

  //中身
}

インスタンス化 は、自分で与えた変数を何か定義すること。
例えば以下のように、整数countは1ですよ。っていうインスタンス化。

 int count = 1; 

コンストラクタ

class名( )の部分。
クラス内のインスタンス変数に初期値をセットできる。

class MyApp {
  MyApp(this.text,this.count); //ここ

  String text;
  int count = 1;
}

他の部分から呼び出すときに、値を渡してやる。
もちろんコンストラクタ内でセットすることもできるが。
MyAppクラスのtextに'テスト'、countに100を渡すなら以下のように使う。

MyApp('テスト',100)

もうちょい詳しく知りたいなら↓
Dartのコンストラクタ

プロパティ

クラスに渡せる値たちのこと。
よく使うScaffoldで言うと、appBarやbodyはScaffoldのプロパティ。
これらはコンストラクタ内で定義されてます。

  Scaffold(
    appBar: AppBar(),
    body: Container()
  );

以下はScaffoldのコンストラクタを抜粋しています。
これらがプロパティとしてScaffoldに渡すことができます。

const Scaffold({
    Key key,
    this.appBar,
    this.body,
    this.floatingActionButton,
    this.floatingActionButtonLocation,
    this.floatingActionButtonAnimator,
    this.persistentFooterButtons,
    this.drawer,
    this.endDrawer,
    this.bottomNavigationBar,
    this.bottomSheet,
    this.backgroundColor,
    this.resizeToAvoidBottomPadding,
    this.resizeToAvoidBottomInset,
    this.primary = true,
    this.drawerDragStartBehavior = DragStartBehavior.start,
    this.extendBody = false,
    this.extendBodyBehindAppBar = false,
    this.drawerScrimColor,
    this.drawerEdgeDragWidth,
    this.drawerEnableOpenDragGesture = true,
    this.endDrawerEnableOpenDragGesture = true,
  })

メソッド

クラス内にある、且つ、処理を記述する部分。
(クラス外にある場合は「関数」)

・メソッド名(triple)は小文字からスタート。
・メソッド名の前に戻り値の型を記述。(ここではint)
・戻り値がない場合はvoidにする

class MyApp {
  MyApp(this.text,this.count);

  String text;
  int count = 1;

  //-------ここ---------
  int triple(int number){       
    return number * 3; 
  }
  //-------ここ---------
}

例えば、3を渡してやると9になります。

print(triple(3));  //結果は9

これはmain'関数'。
なぜならクラス内にないから。

void main(){
  runApp(MyApp());
}

随時、内容は追加していきます。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4