0

I have to initialise the state of widget variable with the value stored in StoredProcedure.

void initState() {
  widget.query = fetchMake();
  super.initState();
}

Future<String> fetchMake() async{
  final prefs = await SharedPreferences.getInstance();
  return prefs.getString(key);
  query.toString();
}

But the issue is that it cant assign that value to query variable and showing error value to type future string cannot assign to string flutter

How can I do that?

2 Answers 2

2

fetchMake is async so you have to put await where you call that method but it will not work because you are calling it in initState.

So You have to assign widget.query variable value in that function only. Moreover, as you get data you have to call setState, so data you receive reflect in ui.

In addition to that you have to check query is null or not where you are using it because when first time build method call it will not have any data.

String query;

void initState() {
  fetchMake();
  super.initState();
}

fetchMake() async{
  final prefs = await SharedPreferences.getInstance();
  setState((){
      query = prefs.getString(key) ?? 'default';
   });
 }
Sign up to request clarification or add additional context in comments.

Comments

2

You can try:

  String query;

  void initState() {
    super.initState();
    fetchMake().then((value) {
      setState(() {
        query = value;
      });
    });
  }

  Future<String> fetchMake() async{
    final prefs = await SharedPreferences.getInstance();
    return prefs.getString(key);
  }

1, You can not set state with widget.query.

2, Create a variable query on state of Widget.

3, fetchMake is a async function => using then to wait result.

2 Comments

Thank you... I tried exactly that but issue is it fetch value in method correctly but query remain null
can you print value of prefs.getString(key), I think SharedPreferences don't have value for you key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.