Guest User

MyApp.dart

a guest
Nov 27th, 2020
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.59 KB | None | 0 0
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:obligatorisk_opgave/util/Launch.dart';
  4. import 'package:obligatorisk_opgave/Services.dart';
  5. import 'package:obligatorisk_opgave/main.dart';
  6.  
  7. class MyApp extends StatefulWidget {
  8.   MyApp({Key key}) : super(key: key);
  9.  
  10.   @override
  11.   _MyAppState createState() => _MyAppState();
  12. }
  13.  
  14. class _MyAppState extends State<MyApp> {
  15.   Future<Launch> futureLaunch;
  16.   List<Launch> launches = List<Launch>();
  17.   bool isLoading = false;
  18.  
  19.   @override
  20.   void initState() {
  21.     super.initState();
  22.     futureLaunch = fetchLaunch() as Future<Launch>;
  23.     isLoading = true;
  24.     Services.getLaunches().then((list) {
  25.       setState(() {
  26.         launches = list;
  27.         isLoading = false;
  28.       });
  29.     });
  30.   }
  31.  
  32.   @override
  33.   Widget build(BuildContext context) {
  34.     return MaterialApp(
  35.         title: 'SpaceX',
  36.         theme: ThemeData(
  37.           primarySwatch: Colors.blue,
  38.         ),
  39.         home: Scaffold(
  40.             appBar: AppBar(
  41.               title: Text(isLoading ? 'Loading...' : 'SpaceX Launches'),
  42.               centerTitle: true,
  43.             ),
  44.             body: Center(
  45.                 child: FutureBuilder<Launch>(
  46.               future: futureLaunch,
  47.               builder: (context, snapshot) {
  48.                 if (snapshot.hasData) {
  49.                   return Text(snapshot.data.missionName);
  50.                 } else if (snapshot.hasError) {
  51.                   return Text('${snapshot.error}');
  52.                 }
  53.                 return CircularProgressIndicator();
  54.               },
  55.             ))
  56.   }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment