How To Use Future Api In Flutter With Code Example In Android App

admin_img Posted By Bajarangi soft , Posted On 30-10-2020

Asynchronous programming is a form of parallel programming that allows a unit of work to run on a different thread than the main application. The thread runs independently from the main app thread and notifies it when it reaches completion or ends in a failure.This approach allows us to do time heavy jobs that if executed on the main thread would bog down the application performance.

How To Use Future Api In Flutter With Code Example In Android App

Future Api
Complete Code For Future Api In Flutter
main.dart

import 'dart:async';
import 'package:flutter/material.dart';
void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Transform',
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool _isLoading = false;

  initApp() async {
    setState(() => _isLoading = true);
    Future.delayed(Duration(seconds: 6)).then((_) {
      setState(() => _isLoading = false);
    });
  }

  @override
  void initState() {
    super.initState();
    initApp();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBar(
        backgroundColor: Colors.amber,
          title: Text("Future Example")),
      body: Center(
          child: _isLoading
              ? CircularProgressIndicator()
              : Text("Future Completed!",
              style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold,color: Colors.white))),
    );
  }
}

Related Post