import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: FibonacciWidget(),
);
}
}
class FibonacciWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: 関数A, //今回はここを変えます
builder: (_, snapshot) {
if (!snapshot.hasData) {
return Scaffold(
appBar: AppBar(
title: const Text("fibonacci"),
),
body: const Center(
child: CircularProgressIndicator(),
),
);
} else {
return Scaffold(
appBar: AppBar(
title: const Text("fibonacci"),
),
body: Center(
child: Text("${snapshot.data}"),
),
);
}
});
}
}
// 非同期でn秒待つだけの関数
Future<String> delayTime(int n) async {
await Future.delayed(Duration(seconds: num));
return "${n}秒経過しました";
}
// 非同期でn番目のフィボナッチ数列を求める関数
Future<int> asyncFibonacci(int n) async {
return n < 2 ? n : (await asyncFibonacci(n - 1) + await asyncFibonacci(n - 2));
}
// 並列処理でn番目のフィボナッチ数列を求める関数
int paraFibonacci(int n) {
return n < 2 ? n : (paraFibonacci(n - 1) + paraFibonacci(n - 2));
}