{"id":69,"date":"2026-04-26T08:40:58","date_gmt":"2026-04-25T23:40:58","guid":{"rendered":"https:\/\/tate.stars.ne.jp\/wpb\/?p=69"},"modified":"2026-04-26T08:42:16","modified_gmt":"2026-04-25T23:42:16","slug":"riverpodtimer","status":"publish","type":"post","link":"https:\/\/tate.stars.ne.jp\/wpb\/riverpodtimer.html","title":{"rendered":"riverpodtimer"},"content":{"rendered":"<pre><code class=\"swift\">\nmain.dart\nimport 'package:flutter\/material.dart';\nimport 'package:flutter_riverpod\/flutter_riverpod.dart';\nimport 'dart:async'; \/\/ Timer\u306b\u5fc5\u8981\nimport 'package:intl\/intl.dart'; \/\/ \u65e5\u4ed8\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u306b\u5fc5\u8981\n\/\/ 1. 1\u79d2\u3054\u3068\u306b\u73fe\u5728\u6642\u523b\u3092Stream\u3067\u6d41\u3059Provider\nfinal dateTimeStreamProvider = StreamProvider<DateTime>((ref) {\n\/\/ periodic\u306f\u6307\u5b9a\u3057\u305fDuration\u3054\u3068\u306b\u51e6\u7406\u3092\u5b9f\u884c\u3059\u308b\nreturn Stream.periodic(const Duration(seconds: 1), (_) => DateTime.now())\n .startWith(DateTime.now()); \/\/ \u6700\u521d\u306e\u4e00\u56de\u306f\u5373\u5ea7\u306b\u6642\u9593\u3092\u8868\u793a\n});\n\nvoid main() {\nrunApp(\n \/\/ Riverpod\u3092\u5229\u7528\u3059\u308b\u305f\u3081\u306bProviderScope\u3067\u56f2\u3080\nconst ProviderScope(child: MyApp()),\n);\n}\nclass MyApp extends StatelessWidget {\nconst MyApp({super.key});\n\n@override\n Widget build(BuildContext context) {\n return MaterialApp(\n home: ClockScreen(),\n );\n}\n}\n\nclass ClockScreen extends ConsumerWidget {\n const ClockScreen({super.key});\n\n @override\n Widget build(BuildContext context, WidgetRef ref) {\n  \/\/ 2. Provider\u3092Watch\u3059\u308b\n final asyncDateTime = ref.watch(dateTimeStreamProvider);\n return Scaffold(\n appBar: AppBar(title: const Text('Riverpod \u73fe\u5728\u6642\u523b')),\n body: Center(\n  child: asyncDateTime.when(\n  data: (dateTime) {\n\/\/ 3. \u65e5\u4ed8\u3068\u6642\u523b\u306e\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\n final dateStr = DateFormat('yyyy\u5e74MM\u6708dd\u65e5 (E)').format(dateTime);\n final timeStr = DateFormat('HH:mm:ss').format(dateTime);\n return Column(\n  mainAxisAlignment: MainAxisAlignment.center,\n  children: [\n  Text(dateStr, style: const TextStyle(fontSize: 24)),\n  const SizedBox(height: 10),\n  Text(timeStr, style: const TextStyle(fontSize: 48, fontWeight: FontWeight.bold)),\n ],\n);\n},\n loading: () => const CircularProgressIndicator(),\n  error: (error, stack) => Text('Error: $error'),\n ),\n ),\n );\n}\n}\n\/\/ Stream\u306e\u6700\u521d\u306e\u8aad\u307f\u8fbc\u307f\u3092\u30b9\u30ad\u30c3\u30d7\u3057\u306a\u3044\u305f\u3081\u306e\u62e1\u5f35\u6a5f\u80fd\nextension StreamExtension<T> on Stream<T> {\n Stream<T> startWith(T value) {\n return Stream.multi((controller) {\n controller.add(value);\n this.listen(controller.add);\n  });\n }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>main.dart import &#8216;package <a href=\"https:\/\/tate.stars.ne.jp\/wpb\/riverpodtimer.html\" class=\"btn btn-link continue-link\">\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":12,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"saved_in_kubio":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-69","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dart"],"_links":{"self":[{"href":"https:\/\/tate.stars.ne.jp\/wpb\/wp-json\/wp\/v2\/posts\/69","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tate.stars.ne.jp\/wpb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tate.stars.ne.jp\/wpb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tate.stars.ne.jp\/wpb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tate.stars.ne.jp\/wpb\/wp-json\/wp\/v2\/comments?post=69"}],"version-history":[{"count":1,"href":"https:\/\/tate.stars.ne.jp\/wpb\/wp-json\/wp\/v2\/posts\/69\/revisions"}],"predecessor-version":[{"id":70,"href":"https:\/\/tate.stars.ne.jp\/wpb\/wp-json\/wp\/v2\/posts\/69\/revisions\/70"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tate.stars.ne.jp\/wpb\/wp-json\/wp\/v2\/media\/12"}],"wp:attachment":[{"href":"https:\/\/tate.stars.ne.jp\/wpb\/wp-json\/wp\/v2\/media?parent=69"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tate.stars.ne.jp\/wpb\/wp-json\/wp\/v2\/categories?post=69"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tate.stars.ne.jp\/wpb\/wp-json\/wp\/v2\/tags?post=69"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}