Skip to content

Instantly share code, notes, and snippets.

@fabian71
Created November 19, 2019 23:07
Show Gist options
  • Select an option

  • Save fabian71/a49d67ecc72f306dd2ec879461f128bb to your computer and use it in GitHub Desktop.

Select an option

Save fabian71/a49d67ecc72f306dd2ec879461f128bb to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:async';
import 'dart:convert';
class Register2 extends StatefulWidget {
@override
_RegisterState2 createState() => _RegisterState2();
}
class _RegisterState2 extends State<Register2> {
Estados _current;
//final String uri = 'https://jsonplaceholder.typicode.com/users';
final String uri = 'http://www.deltainternet.net.br/api/v1.0/states';
Future<List<Estados>> _fetchUsers() async {
var response = await http.get(uri);
if (response.statusCode == 200) {
final items = json.decode(response.body).cast<Map<String, dynamic>>();
List<Estados> listOfUsers = items.map<Estados>((json) {
return Estados.fromJson(json);
}).toList();
return listOfUsers;
} else {
throw Exception('Failed to load internet');
}
}
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
GlobalKey<FormState> _formKey = GlobalKey<FormState>();
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 0.0,
title: Text(''),
iconTheme: IconThemeData(
color: Colors.purple,
),
),
body: Container(
color: Colors.white24,
child: SingleChildScrollView(
child: Form(
key: _formKey,
autovalidate: true,
child: Column(
children: <Widget>[
FutureBuilder<List<Estados>>(
future: _fetchUsers(),
builder: (BuildContext context,
AsyncSnapshot<List<Estados>> snapshot) {
if (!snapshot.hasData)
return CircularProgressIndicator();
return DropdownButton<Estados>(
items: snapshot.data
.map((data) => DropdownMenuItem<Estados>(
child: Text(data.name),
value: data,
))
.toList(),
onChanged: (Estados value) {
setState(() {
_current = value;
});
},
isExpanded: true,
//value: _current,
hint: Text('Selecione o estado'),
);
}),
SizedBox(height: 20.0),
_current != null
? Text("Estado: " + _current.name)
: Text("Nenum estado selecionado"),
],
),
),
)),
);
}
}
class Estados {
int id;
String name;
Estados({
this.id,
this.name,
});
factory Estados.fromJson(Map<String, dynamic> json) {
return Estados(
id: json['id'],
name: json['name'],
);
}
}
@hrqlp
Copy link

hrqlp commented Nov 22, 2019

Estou tendo um problema semelhante, estou tentando usar o seguinte JSON em
dropdown já tentei de diversas formas, tanto como http, quanto por assets, não consigo obter sucesso., https://raw.githubusercontent.com/hrqlp/byte/master/lib/uf_dados.json

@fabian71
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment