future widget flutter code example

Example 1: flutter future builder

FutureBuilder<String>(
        future: _fetchNetworkCall, // async work
        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 stateful widget

class YellowBird extends StatefulWidget {
  const YellowBird({ Key key }) : super(key: key);

  
  _YellowBirdState createState() => _YellowBirdState();
}

class _YellowBirdState extends State<YellowBird> {
  
  Widget build(BuildContext context) {
    return Container(color: const Color(0xFFFFE306));
  }
}

Example 3: make stateful widget flutter

class MyClass extends StatefulWidget {
	
    MyClassState createState() => new MyClassState();
}

class MyClassState extends State<MyClass> {
	//Widgets and other code here
}

Example 4: future builder flutter

Example of future function:Future getProjectDetails() async {  List<ProjectModel> projetcList = await someFutureCall();  return projetcList;    }

Tags:

Dart Example