-1

I have an err in my app. I can't read data from shared perfrences in flutter

my code

TextFormField(
              enabled: false,
             initialValue:  Data.getData(key: 'phone_number'),
                    maxLength: 14,
                    keyboardType: TextInputType.phone,
                    textDirection: TextDirection.ltr,
                    decoration: InputDecoration(
                      focusedBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.deepPurple),
                      ),
                      suffixIcon: Icon(
                        Icons.phone_android_sharp,
                        color: Colors.deepPurple,
                      ),
                      border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(30.0)),
)),

my getData func

static getData({key}) async {
    final perf = await SharedPreferences.getInstance();
    var data = perf.getString(key);
    print(data);
    return data.toString();
  }
Hadizhp
  • 5
  • 1
  • Does this answer your question? [What is a Future and how do I use it?](https://stackoverflow.com/questions/63017280/what-is-a-future-and-how-do-i-use-it) – nvoigt Aug 17 '21 at 05:24
  • Getting data from shared preferences is an async operation, so take a look at [FutureBuilder](https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html) – Mol0ko Aug 17 '21 at 06:45

2 Answers2

1

As you are returning a String value and since you haven't specified the return type you are getting this error.

Please replace your current getData function with the below function, hopefully, this should fix the issue.

static Future<String> getData({key}) async {
    final perf = await SharedPreferences.getInstance();
    var data = perf.getString(key);
    print(data);
    return data.toString();
  }
xpetta
  • 607
  • 12
  • 28
1

Declare a variable initialValue and then assign it the value by calling the function getData to it as follows:-

String initialValue='';
@override
void initState(){
   super.initState();
   initializeData();
}

void initializeData()async{
  setState((){
    initialValue=await Data.getData(key: 'phone_number');
  });
}

and then simply put this initialValue in your below code as:-

TextFormField(
              enabled: false,
             initialValue:  initialValue,
                    maxLength: 14,
                    keyboardType: TextInputType.phone,
                    textDirection: TextDirection.ltr,
                    decoration: InputDecoration(
                      focusedBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.deepPurple),
                      ),
                      suffixIcon: Icon(
                        Icons.phone_android_sharp,
                        color: Colors.deepPurple,
                      ),
                      border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(30.0)),
)),
Deepak Lohmod
  • 2,072
  • 2
  • 8
  • 18