Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I am always getting "AsyncSnapshot<String>(ConnectionState.none, null, null, null)" in FutureBuilder

I am getting a connection state as none when trying to get the data from locally saved JSON.
what I trying to do is build the dropdown widget with the JSON as dropdown values

class _TestState extends State<Test> {
  var stateList;

  initState() {
    _getStateList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
          child: FutureBuilder(
        future: _getStateList(), // async work

        builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
          print(snapshot);
          switch (snapshot.connectionState) {
            case ConnectionState.none:
              return Text('Press button to start.');
            case ConnectionState.active:
            case ConnectionState.waiting:
              return Text('Awaiting result...');
            case ConnectionState.done:
              if (snapshot.hasError) return Text('Error: ${snapshot.error}');
              return Text('Result: ${snapshot.data}');
            // You can reach your snapshot.data['url'] in here
          }
        },
      )),
    );
  }

  _getStateList() {
    // setState(() {
    DetailServices().stateJson().then((res) => {stateList = res});
    // });
  }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You need a Future<String> for the future property. So, change the following code:

  _getStateList() {
    // setState(() {
    DetailServices().stateJson().then((res) => {stateList = res});
    // });
  }

to:

  Future<String> _getStateList() {
    return DetailServices().stateJson();
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading