Skip to content

Instantly share code, notes, and snippets.

@mobibob
Created August 9, 2021 18:50
Show Gist options
  • Save mobibob/7a86eaccb35e88d40592a8acdd6dd578 to your computer and use it in GitHub Desktop.
Save mobibob/7a86eaccb35e88d40592a8acdd6dd578 to your computer and use it in GitHub Desktop.
From a tutorial, this simple app covers getting a JSON response from an HTTP get request and displaying the content in a scrolling ListView.
/// NOTE: Need some yamspec.yml to complete from a default configuration.
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:async';
import 'dart:io';
void main() {
runApp(new MaterialApp(home: new MyApp()));
}
class MyApp extends StatefulWidget {
@override
_State createState() => new _State();
}
class _State extends State<MyApp> {
Map _countries = new Map();
void _getData() async {
String url = "http://country.io/names.json"; // as Uri; //Uri.('http://country.io/names.json');
var response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
setState(() => _countries = jsonDecode(response.body));
var verbose = true;
print('Loaded ${_countries.length} countries.');
}
}
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Udemy List view"),
),
body: new Container(
padding: EdgeInsets.all(32.0),
child: new Column(
children: <Widget>[
Text("${_countries.length} Countries"),
new Expanded(
child: new ListView.builder(
itemCount: _countries.length,
itemBuilder: (BuildContext context, int index) {
String key = _countries.keys.elementAt(index);
return new Row(
children: <Widget>[
new Text("$key : "),
new Text(_countries[key])
]
);
},
))
],
),
));
}
@override
void initState() {
super.initState();
_getData();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment