Stream builder

12 Oct 2024  Khlee  1 min read.

플러터 강좌를 보고 정리한 내용입니다.

Stream<T>

Stream<T>은 데이터의 흐름이 발행되는 객체이다.

Stream<int> addStreamValue() {
  return Stream<int>.periodic(
    Duration(seconds: 1),
    (count) {
      return price + count;
    },
  );
}

StreamBuilder

StreamBuilderStream<T>에 따라 내용을 업데이트할 수 있는 위젯이다.

StreamBuilder<int>(
  initialData: price,
  stream: addStreamValue(),
  builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
    final priceNumber = snapshot.data.toString();
    return Center(
      child: Text(
        priceNumber,
        style: TextStyle(
          fontWeight: FontWeight.bold,
          fontSize: 40,
          color: Colors.blue,
        ),
      ),
    );
  },
),

khlee
khlee