Flutter: Passing data to StatefulWidgets

Flutter: Passing data to StatefulWidgets

If you want to pass data to your StatefulWidget in flutter you pass the data to the widget itself and store it there and access it via widget.XXX from your state.

e.g.

widget.song

See a small sample.

import 'package:flutter/material.dart';
import 'package:signin_example/app/services/song.dart';

class SongDetails extends StatefulWidget {
  final Song song;

  SongDetails({this.song});

  @override
  _SongDetailsState createState() => _SongDetailsState();
}

class _SongDetailsState extends State<SongDetails> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.song.name),
        
      ),
      body: Text(""),
    );
  }
}