LoginSignup
30
19

More than 5 years have passed since last update.

Flutterクラス間の値の受け渡し

Last updated at Posted at 2019-01-28
main.dart

class _MyPageState extends State<MyPage> {

//省略

//受け渡したい変数
final String watashitai;

  Widget _buildListItem(BuildContext context, DocumentSnapshot document) {
    return Card(
      child: Column(mainAxisSize: MainAxisSize.min, children: <Widget>[

       //渡したい変数を引数に指定する
        ImageUrl(imageUrl: watashitai)

      ]),
    );
  }
}

class ImageUrl extends StatelessWidget {

//受け取る値を代入する変数を定義
  final String imageUrl;

//受け取る値を上記の変数に代入
  ImageUrl({this.imageUrl});

}

_MyPageStateクラスで定義されている変数watashitaiをImageUrlクラスでも使えるようにしている。

詳細

以下の行の内容。
渡すクラス(渡した時に代入したい変数 : 渡したい変数)

main.dart
ImageUrl(imageUrl: watashitai)

以下の1行目で変数定義。
2行目でコンストラクタを使いimageUrl変数に受け取る値を代入している。

main.dart
//受け取る値を代入する変数を定義
  final String imageUrl;

//受け取る値を上記の変数に代入
  ImageUrl({this.imageUrl});

これでimageUrlの中に別クラスにあったwatashitai変数が代入されて、別クラス間での値の受け渡しができた。

30
19
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
30
19