|
| 1 | +/// |
| 2 | +/// Created by NieBin on 2019/6/13 |
| 3 | +/// Github: https://github.com/nb312 |
| 4 | +/// Email: niebin312@gmail.com |
| 5 | +
|
| 6 | +import "package:flutter/material.dart"; |
| 7 | +import "package:flutter_widgets/const/_const.dart"; |
| 8 | + |
| 9 | +class FuturePage extends StatefulWidget { |
| 10 | + @override |
| 11 | + _FutureState createState() => _FutureState(); |
| 12 | +} |
| 13 | + |
| 14 | +class _FutureState extends State<FuturePage> { |
| 15 | + Future<String> _getData() async { |
| 16 | + return "Hello world"; |
| 17 | + } |
| 18 | + |
| 19 | + @override |
| 20 | + Widget build(BuildContext context) { |
| 21 | + return Scaffold( |
| 22 | + appBar: AppBar( |
| 23 | + title: Text("Hello world"), |
| 24 | + ), |
| 25 | + body: Container( |
| 26 | + child: FutureBuilder<String>( |
| 27 | + future: _getData(), // a previously-obtained Future<String> or null |
| 28 | + builder: (BuildContext context, AsyncSnapshot<String> snapshot) { |
| 29 | + switch (snapshot.connectionState) { |
| 30 | + case ConnectionState.none: |
| 31 | + return Text('Press button to start.'); |
| 32 | + case ConnectionState.active: |
| 33 | + case ConnectionState.waiting: |
| 34 | + return Text('Awaiting result...'); |
| 35 | + case ConnectionState.done: |
| 36 | + if (snapshot.hasError) return Text('Error: ${snapshot.error}'); |
| 37 | + return Text('Result: ${snapshot.data}'); |
| 38 | + } |
| 39 | + return RefreshIndicator( |
| 40 | + child: null, onRefresh: null); // unreachable |
| 41 | + }, |
| 42 | + ), |
| 43 | + ), |
| 44 | + ); |
| 45 | + } |
| 46 | +} |
0 commit comments