futurebuilder flutter code example
Example 1: flutter future builder
FutureBuilder<String>(
future: _fetchNetworkCall,
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting: return Text('Loading....');
default:
if (snapshot.hasError)
return Text('Error: ${snapshot.error}');
else
return Text('Result: ${snapshot.data}');
}
},
),
Example 2: flutter future builder
Future _getData;
@override
void initState() {
_getData = _getUserData();
super.initState();
}
_getUserData() async{
return await getAsyncDataFromServer();
}
FutureBuilder(
future: _getData;
builder: (BuildContext context, AsyncSnapshot snapshot){
if(snapshot.connectionState == ConnectionState.done){
}else{
return Center(child: CircularProgressIndicator());
}
},
);
Example 3: future builder flutter
Example of future function:Future getProjectDetails() async { List<ProjectModel> projetcList = await someFutureCall(); return projetcList; }
Example 4: future builder flutter
class ProjectModel { String id; String createdOn; String lastModifiedOn; String title; String description; ProjectModel({ this.id, this.createdOn, this.lastModifiedOn, this.title, this.description, });}