Search
 
SCRIPT & CODE EXAMPLE
 

DART

perform async function in widget build method

import 'package:flutter/material.dart';

class FooWidget extends StatefulWidget {
  @override
  _FooWidgetState createState() => _FooWidgetState();
}

class _FooWidgetState extends State<FooWidget> {
  Future<int> _bar;

  @override
  void initState() {
    super.initState();
    _bar = doSomeLongRunningCalculation();
  }

  void _retry() {
    setState(() {
      _bar = doSomeLongRunningCalculation();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        FutureBuilder<int>(
          future: _bar,
          builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
            if (snapshot.hasData) {
              return Text('The answer to everything is ${snapshot.data}');
            } else {
              return Text('Calculating answer...');
            }
          },
        ),
        RaisedButton(
          onPressed: _retry,
          child: Text('Retry'),
        )
      ],
    );
  }
}

Future<int> doSomeLongRunningCalculation() async {
  await Future.delayed(Duration(seconds: 5)); // wait 5 sec
  return 42;
}
Comment

PREVIOUS NEXT
Code Example
Dart :: how to check string id is valid id in string file android studio 
Dart :: flutter cachImage 
Dart :: dart svg drawer 
Dart :: flutter button sound effects 
Dart :: flutter signing the app 
Dart :: flutter sliver persistent header example 
Dart :: flutter ignore keyboard 
Dart :: flutter sidebox 
Dart :: expand contanner in signlescroll flutter 
Dart :: flutter conditional parent widget 
Dart :: how to stop listening to location change listener on dispose in flutter 
Dart :: flutter const advantag 
Dart :: flutter padding symmetric 
Dart :: dart code examples 
Swift :: dart capitalize first letter of each word 
Swift :: swift ui check if number is a prime 
Swift :: how to replace certain characters in string swift 
Swift :: UI API called on a background thread 
Swift :: swift function with return value 
Swift :: swiftui navigation link with button 
Swift :: swift programming language wikipedia 
Swift :: how to convert int to double in swiftui 
Swift :: swift how to animate constraint change 
Swift :: How to set back button text in Swift 
Swift :: swift animate add subview 
Swift :: how to change the color of back button navbar xcodee 
Swift :: swift dictionary contains key 
Swift :: swiftui popover 
Swift :: how to bold 1 word swift 
Swift :: how to set return type swift 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =