snapshot.hasData is returning null and the drop button never gets displayed.
I am trying to fetch data from the api and display it in a dropdown button. the response.body is fetched correctly as it gets printed when i try to print it. But when I try to print listOfOperators, nothing is printed. Looks like the listOfOperators is empty, hence the snapshot doesnt have any data. I am not able to figure out why this is the case
JsonData:
{"status": "success", "OperatorType": "GAS", "data": {"MAHANAGAR_GAS_LTD": {"name": "Mahanagar Gas Limited", "authenticator3": null, "label": "Customer Account Number (12 digits start with 21)", "note": null, "account_label": "Bill Group Number (1 - 8 characters + digits)", "authenticator3_options": null}, "TRIPURA_NATURAL_GAS_COMPANY_LTD": {"name": "Tripura Natural Gas Company Ltd", "authenticator3": null, "label": "Consumer Number (1 - 20 digits)", "note": null, "account_label": null, "authenticator3_options": null}, "HARAYANA_CITY_GAS": {"name": "Haryana City gas", "authenticator3": null, "label": "CRN Number (8-12 digits)", "note": null, "account_label": null, "authenticator3_options": null}, "GUJARAT_GAS_COMPANY_LTD": {"name": "Gujarat Gas company Limited", "authenticator3": null, "label": "Service Number (1-15 digits)", "note": null, "account_label": null, "authenticator3_options": null}, "VADODARA_GAS_LTD": {"name": "Aavantika Gas Ltd", "authenticator3": null, "label": "Customer Number (10-15 Alphanumeric)", "note": null, "account_label": null, "authenticator3_options": null}, "SGL": {"name": "Sabarmati Gas Limited (SGL)", "authenticator3": null, "label": "Customer ID (12 digits)", "note": null, "account_label": null, "authenticator3_options": null}, "SITI_ENERGY": {"name": "Siti Energy", "authenticator3": null, "label": "ARN Number (7 - 9 digits)", "note": null, "account_label": null, "authenticator3_options": null}, "UCPGPL": {"name": "Unique Central Piped Gases Pvt Ltd (UCPGPL)", "authenticator3": null, "label": "Customer Number (8 digits + character)", "note": null, "account_label": null, "authenticator3_options": null}, "IGL": {"name": "IGL (Indraprasth Gas Limited)", "authenticator3": null, "label": "Consumer Number (10 digits)", "note": null, "account_label": null, "authenticator3_options": null}, "ADANI_GAS": {"name": "ADANI GAS", "authenticator3": null, "label": "Customer ID (10 digits)", "note": null, "account_label": null, "authenticator3_options": null}}}
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
class JsonApiDropdown extends StatefulWidget {
@override
JsonApiDropdownState createState() {
return new JsonApiDropdownState();
}
}
class JsonApiDropdownState extends State<JsonApiDropdown> {
Operators _currentOperator;
final String uri = "https://stage.linq.store/recharge-bill-payments?load=GetOperators&type=GAS";
Future<List<Operators>> _fetchdata() async {
var response = await http.get(uri,
headers: {
"Content-Type" : "application/json",
"MyToken" : "Token d5c9912f-4d4a-4776-88c4-545779804040",
"Request-Type" : "mobile_api",
}
);
if (response.statusCode == 200) {
final operators = jsonDecode(response.body).cast<Map<String, dynamic>>();
List<Operators> listOfOperators = operators.map<Operators>((json) {
return Operators.fromJson(json);
}).toList();
return listOfOperators;
} else {
throw Exception('Failed to load internet');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Gas Bill Payment'),
backgroundColor: Colors.orangeAccent,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
FutureBuilder<List<Operators>>(
future: _fetchdata(),
builder: (BuildContext context,
AsyncSnapshot<List<Operators>> snapshot) {
if (!snapshot.hasData) return CircularProgressIndicator();
return DropdownButton<Operators>(
items: snapshot.data
.map((operator) => DropdownMenuItem<Operators>(
child: Text(operator.name),
value: operator,
))
.toList(),
onChanged: (Operators value) {
setState(() {
_currentOperator = value;
});
},
isExpanded: false,
value: _currentOperator,
hint: Text('Select Operator'),
);
}),
SizedBox(height: 20.0),
_currentOperator != null
? Text("Name: " +
_currentOperator.name +
"\n Label: " +
_currentOperator.label +
"\n Account Label: " +
_currentOperator.accountLabel)
: Text("No Operator selected"),
],
),
),
);
}
}
class Operators {
String name;
String label;
String accountLabel;
Operators({
this.name,
this.label,
this.accountLabel
});
factory Operators.fromJson(Map<String, dynamic> json) {
return Operators(
name: json['name'],
label: json['label'],
accountLabel: json['account_label'],
);
}
}