I was trying to populate json array data into dropdown and it worked fine. And after I tried to replace json array with json api. after replacing with api it doesn't work. This may a simple problem but it seems hard to me...i'm totally new to flutter. I couldn't find a proper tutorial.please give me the way to do it. below is my code
home.dart
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:chart_test1/countryList.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Countries countries;
List<Countries> _list = [];
String selectedRegion;
@override
Widget build(BuildContext context) {
final json = JsonDecoder().convert(countries);
//here counttries is a problem
_list = (json).map<Countries>((item) => Countries.fromJson(item)).toList();
selectedRegion = _list[0].iso2;
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
DropdownButtonHideUnderline(
child: new DropdownButton<String>(
hint: new Text("Select Region"),
value: selectedRegion,
isDense: true,
onChanged: (String newValue) {
setState(() {
selectedRegion = newValue;
});
print(selectedRegion);
},
items: _list.map((Countries map) {
return new DropdownMenuItem<String>(
value: map.iso2,
child: new Text(map.country,
style: new TextStyle(color: Colors.black)),
);
}).toList(),
),
),
],
),
),
);
}
}
apiData.dart
import 'dart:async';
import 'package:http/http.dart' as http;
import 'dart:convert';
class Countries {
final String country;
final String slug;
final String iso2;
Countries({this.country, this.slug, this.iso2});
factory Countries.fromJson(Map<String, dynamic> json) {
return Countries(
country: json['country'],
slug: json['slug'],
iso2: json['iso2'],
);
}
Future<List<Countries>> getAllCountries() async {
var data = await http.get("https://api.covid19api.com/countries");
var jsonData = json.decode(data.body);
print(jsonData);
return jsonData;
}
}
Thank you!
