Flutter:Instance of 'SharedPreferences'

Viewed 181

I was using shared_preferences plugin in my Flutter application, I want to save data when the user selects the city. But when I try to print it just says;

Instance of 'SharedPreferences' Unhandled Exception: setState() callback argument returned a Future

. (Even if I remove my setState part I get the same error)in my console.

does anyone know the reason??

My text in the in card widget

 Padding(
                                padding: const EdgeInsets.symmetric(
                                    horizontal: 8.0),
                                child: Text(
                                  snapshot.data.name,
                                  style: TextStyle(fontSize: 16),
                                ),
                              ),

and when clicked where I saved it

   onTap: () {
                            setState(() async {
                              final country =
                                  await SharedPreferences.getInstance();
                              String name;
                              name=snapshot.data.name;
                              country.setString('name', name);
                              print('here $country');
                          
                            });
                          },
1 Answers

Try

 onTap: () async {
  final country =
      await SharedPreferences.getInstance();
  setState(() {
  String name;
  name=snapshot.data.name;
  country.setString('name', name);
  print('here $name);

  });
},
Related