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

Time:11-27

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});
    // });
  }
}

CodePudding user response:

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();
  }

CodePudding user response:

the issue is _getStateList doesn't return anything it should either return a future or you create a field that is the future and set that in _getStateList

I have an old article on medium that's about Asynchronously changing the UI which might help you Article Link

  • Related