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

FutureBuilder Good Example 1

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)
20 views2 pages

FutureBuilder Good Example 1

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(const MaterialApp(home: HomePage()));
}

class HomePage extends StatelessWidget {


const HomePage({Key? key}) : super(key: key);

Future<String> getWriterName() {
return Future.delayed(const Duration(seconds: 3), () => "Maria Elijah");
}

@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
backgroundColor: Colors.deepPurpleAccent,
title: const Text('Flutter FutureBuilder'),
),
body: SizedBox(
width: double.infinity,
child: Center(
child: FutureBuilder(
future: getWriterName(),
initialData: "Code sample",
builder: (BuildContext context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(
color: Colors.deepPurpleAccent,
),
);
}
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
return Center(
child: Text(
'An ${snapshot.error} occurred',
style: const TextStyle(fontSize: 18, color: Colors.red),
),
);
} else if (snapshot.hasData) {
final data = snapshot.data;
return Center(
child: Text(
data!,
style: const TextStyle(
fontSize: 20, fontWeight: FontWeight.bold),
),
);
}
}

return const Center(


child: CircularProgressIndicator(),
);
},
),
),
),
));
}
}
//https://www.educative.io/answers/what-is-a-futurebuilder

You might also like