Answers for "how to call function when opening app flutter"

0

how to call function when opening app flutter

// Check out the source page for step-by-step walkthrough.

import 'package:flutter/material.dart';
 
void main() => runApp(MyApp());
 
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
           appBar: AppBar(
            title: Text('Call a Function Everytime When App Starts')), 
            body: Center(
            child: Alert()
           )
          )
        );
  }
}
 
class Alert extends StatefulWidget {
 
  AlertState createState() => AlertState();
 
}
 
class AlertState extends State {
 
  @override
  void initState() {
    showAlert(context);
    super.initState();
  }
 
  Future showAlert(BuildContext context) async {
 
    await Future.delayed(Duration(seconds: 6));
 
    showDialog(
    context: context,
    builder: (BuildContext context) {
      return AlertDialog(
        title: new Text('Welcome To Our App :) .'),
        actions: <Widget>[
          FlatButton(
            child: new Text("OK"),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
        ],
      );
     },
    );
  }
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text('Call A Function Automatically When App Starts Everytime',
        style: TextStyle(fontSize: 22,), textAlign: TextAlign.center,)
      ),
    );
  }
}
Posted by: Guest on January-19-2022

Code answers related to "how to call function when opening app flutter"

Browse Popular Code Answers by Language