Created
March 30, 2023 04:25
-
-
Save take4blue/8769d8f82f1b5c9237c89914deb9d4bb to your computer and use it in GitHub Desktop.
FutureBuilderのサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
class FutureRefresh7 extends StatefulWidget { | |
const FutureRefresh7({super.key}); | |
@override | |
State<FutureRefresh7> createState() => _FutureRefresh7State(); | |
} | |
class _FutureRefresh7State extends State<FutureRefresh7> { | |
late Future<void> future; | |
static Future<void> generateSupply(int loop) async { | |
for (int i = 0; i < loop; i++) { | |
await Future.delayed(const Duration(milliseconds: 1)); | |
if ((i % 100) == 0) { | |
debugPrint("$i"); | |
} | |
} | |
debugPrint("Done"); | |
} | |
@override | |
void initState() { | |
future = generateSupply(1000); | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text("FutureRefresh"), | |
), | |
body: FutureBuilder<void>( | |
future: future, | |
builder: (context, snapshot) { | |
switch (snapshot.connectionState) { | |
case ConnectionState.none: | |
return const Center(child: Text("none")); | |
case ConnectionState.waiting: | |
return Container( | |
alignment: Alignment.center, | |
child: const CircularProgressIndicator()); | |
case ConnectionState.active: | |
return const Center(child: Text("active")); | |
case ConnectionState.done: | |
return const Center(child: Text("done")); | |
} | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment