0% found this document useful (0 votes)
28 views2 pages

FutureBuilder Good Example 3

About future builder

Uploaded by

Bharath Krishnan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views2 pages

FutureBuilder Good Example 3

About future builder

Uploaded by

Bharath Krishnan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import 'package:flutter/material.

dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Woolha.com Flutter Tutorial',
home: FutureBuilderExample(),
debugShowCheckedModeBanner: false,
);
}
}

Future<String> getValue() async {


await Future.delayed(Duration(seconds: 5));

return 'Woolha';
}

class FutureBuilderExample extends StatefulWidget {


@override
State<StatefulWidget> createState() {
return _FutureBuilderExampleState ();
}
}

class _FutureBuilderExampleState extends State<FutureBuilderExample> {

Future<String>? _value;

@override
initState() {
super.initState();
_value = getValue();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Woolha.com Flutter Tutorial'),
),
body: SizedBox(
width: double.infinity,
child: Center(
child: FutureBuilder<String>(
future: _value,
initialData: 'App Name',
builder: (
BuildContext context,
AsyncSnapshot<String> snapshot,
) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator(),
Visibility(
visible: snapshot.hasData,
child: Text(
snapshot.data!,
style: const TextStyle(color: Colors.black, fontSize:
24),
),
)
],
);
} else if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
return const Text('Error');
} else if (snapshot!.hasData) {
return Text(
snapshot.data!,
style: const TextStyle(color: Colors.teal, fontSize: 36)
);
} else {
return const Text('Empty data');
}
} else {
return Text('State: ${snapshot.connectionState}');
}
},
),
),
),
);
}
}
//https://www.woolha.com/tutorials/flutter-using-futurebuilder-widget-examples

You might also like