Advertisement
thinkdigital

Dart Quake App - Issues with Async

May 22nd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.49 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'dart:async';
  3. import 'dart:convert';
  4. import 'package:http/http.dart' as http;
  5. import 'package:intl/intl.dart';
  6.  
  7. class Quake extends StatefulWidget {
  8.   @override
  9.   State<StatefulWidget> createState() => new QuakeState();
  10. }
  11.  
  12. class QuakeState extends State<Quake> {
  13.     // https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson
  14.     final String url = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson";
  15.  
  16.   @override
  17.   Widget build(BuildContext context) {
  18.     List earthquakes;
  19.     var _data;
  20.  
  21.     return new Scaffold(
  22.         appBar: new AppBar(title: new Text("Quakes - USGS All Earthquakes"),
  23.         backgroundColor: Colors.red,
  24.         centerTitle: true,),
  25.         body: new ListView.builder(
  26.             itemBuilder: (BuildContext context, int index) {
  27.               _data = getJson(url).then((onValue){
  28.                 earthquakes = onValue['features'];
  29.  
  30.               });
  31.               var time = earthquakes != null ? earthquakes[index]['properties']['time']: "";
  32.               time *= 1000;
  33.               var dateTime = new DateTime.fromMillisecondsSinceEpoch(int.parse(time));
  34.               time = new DateFormat.yMMMMd(dateTime).add_jm();
  35.               return new ListTile(
  36.                 title: new Text(time ?? ""),
  37.               );
  38.             }));
  39.   }
  40. }
  41.  
  42. Future<Map> getJson(String url) async {
  43.   var response = await http.get(url);
  44.   return json.decode(response.body);
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement