共享偏好:值未在实时或立即更新

发布于 2025-02-11 00:53:55 字数 7266 浏览 1 评论 0原文

我正在尝试实现用户在一个屏幕上提供输入的功能,并且数据在本地存储(共享的首选项),并且一旦用户返回另一个屏幕或弹出,则应立即将数据从该屏幕上更新到该屏幕。上一个屏幕。当前发生的事情是,仅当我导航到另一个屏幕并导航回该屏幕时,数据才会更新。

SharedPreferences
import 'package:shared_preferences/shared_preferences.dart';


class UserSimplePreferences {
  static late SharedPreferences _preferences;
  static const _name = 'name';
  static const _age = 'age';
  static const _gender = 'gender';
  static const _church = 'church';
  static const _churchCity = 'churchCity';
  static const _churchState = 'churchState';


  static Future init() async =>
      _preferences = await SharedPreferences.getInstance();

  static Future setName(String name) async =>
      await _preferences.setString(_name, name);

  static String? getName() => _preferences.getString(_name);

  static Future setAge(String age) async =>
      await _preferences.setString(_age, age);

  static String? getAge() => _preferences.getString(_age);

  static Future setGender(String gender) async =>
      await _preferences.setString(_gender, gender);

  static String? getGender() => _preferences.getString(_gender);

  static Future setChurch(String church) async =>
      await _preferences.setString(_gender, church);

  static String? getChurch() => _preferences.getString(_church);


  static Future setChurchCity(String churchCity) async =>
      await _preferences.setString(_gender, churchCity);

  static String? getChurchCity() => _preferences.getString(_churchCity);


  static Future setChurchState(String churchState) async =>
      await _preferences.setString(_gender, churchState);

  static String? getChurchState() => _preferences.getString(_churchState);


}


Register(Here the data should be updated)

class Register extends StatefulWidget {


  @override
  _RegisterState createState() => _RegisterState();
}

class _RegisterState extends State<Register> {
  late String? name = UserSimplePreferences.getName();

 // final user = UserSimplePreferences.getUser();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
     }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
    body:Container(
        child:  Column(
          children: [
            Card(
              child: Text(name??''),
            ),
            Card(
              child: Text(name??''),
            ),
            Card(
              child: Text(name??''),
            )
          ],

       )
      ),
      floatingActionButton: FloatingActionButton(
        backgroundColor: Colors.indigo[900],
        onPressed: () {
          Navigator.push(
            context,
            MaterialPageRoute(builder: (_) {
              return AddUser(idUser: '',);
            }),
          );
        },
        child: Icon(Icons.add, color: Colors.white),
      ),
    );

  }
}



AddUser(where user provides input)
class AddUser extends StatefulWidget {

  final String? idUser;


  const AddUser({required this.idUser});

  @override
  _AddUserState createState() => _AddUserState();

}

class _AddUserState extends State<AddUser> {

  final formKey = GlobalKey<FormState>();
  String name = '';
 String age = '';
 String gender= '';
 String church= '';
 String  churchCity= '';
  String churchState= '';

  @override
  void initState() {
    super.initState();

    // name = UserSimplePreferences.getName() ?? '';
    // age = UserSimplePreferences.getAge() ?? '';
    // gender = UserSimplePreferences.getGender() ?? '';
    // church = UserSimplePreferences.getChurch() ?? '';
    //  churchCity = UserSimplePreferences.getChurchCity() ?? '';
    // churchState = UserSimplePreferences.getChurchState() ?? '';

  }

  @override
  Widget build(BuildContext context) => Scaffold(
    appBar: AppBar(
      automaticallyImplyLeading: false,
      title : Text('Please Enter Your Details',textAlign: TextAlign.center,)),

    body: SafeArea(
          child: ListView(
        padding: EdgeInsets.all(16),
        children: [

          buildName(),
          const SizedBox(height: 12),
          buildAge(),
          const SizedBox(height: 12),
          buildGender(),
          const SizedBox(height: 12),
          buildChurch(),
          const SizedBox(height: 12),
          buildChurchCity(),
          const SizedBox(height: 12),
          buildChurchState(),
          const SizedBox(height: 12),

          buildButton(),
        ],
      ),
    ),
  );

  Widget buildName() =>

    buildTitle(
    title: 'Name',
    child: TextFormField(
      initialValue: name,
      decoration: InputDecoration(
        border: OutlineInputBorder(),
        hintText: 'Your Name',
      ),
      onChanged: (name) => setState(() => this.name = name),
    ),
  );

  Widget buildAge() => buildTitle(
    title: 'Age',
    child: TextFormField(
      initialValue: age,
      decoration: InputDecoration(
        border: OutlineInputBorder(),
        hintText: 'Your Age',
      ),
      onChanged: (age) => setState(() => this.age = age),
    ),
  );
  Widget buildGender() => buildTitle(
    title: 'Gender',
    child: TextFormField(
      initialValue: gender,
      decoration: InputDecoration(
        border: OutlineInputBorder(),
        hintText: 'Your gender(M/F)',
      ),
      onChanged: (gender) => setState(() => this.gender = gender),
    ),
  );
  Widget buildChurch() => buildTitle(
    title: 'Church',
    child: TextFormField(
      initialValue: church,
      decoration: InputDecoration(
        border: OutlineInputBorder(),
        hintText: 'Your church',
      ),
      onChanged: (church) => setState(() => this.church = church),
    ),
  );
  Widget buildChurchCity() => buildTitle(
    title: 'Church City',
    child: TextFormField(
      initialValue: churchCity,
      decoration: InputDecoration(
        border: OutlineInputBorder(),
        hintText: 'Your Church City',
      ),
      onChanged: (churchCity) => setState(() => this.churchCity = churchCity),
    ),
  );
  Widget buildChurchState() => buildTitle(
    title: 'Church State',
    child: TextFormField(
      initialValue: churchState,
      decoration: InputDecoration(
        border: OutlineInputBorder(),
        hintText: 'Your Church State',
      ),
      onChanged: (churchState) => setState(() => this.churchState = churchState),
    ),
  );


  Widget buildButton() => ButtonWidget(
      text: 'Save',
      onClicked: () async {

        await UserSimplePreferences.setName(name);
        await UserSimplePreferences.setAge(age);
        await UserSimplePreferences.setGender(gender);
        await UserSimplePreferences.setChurch(church);
        await UserSimplePreferences.setChurchCity(churchCity);
        await UserSimplePreferences.setChurchState(churchState);
             Navigator.pop(context);
      });

  Widget buildTitle({
    required String title,
    required Widget child,
  }) =>
      Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Text(
            title,
            style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
          ),
          const SizedBox(height: 8),
          child,
        ],
      );
}

I am trying to implement a feature where the user provides input on one screen and the data is stored locally (Shared Preferences) and as soon as the user gets back to another screen or pops back, the data should be updated to that screen immediately from the previous screen. Currently what is happening is, the data is updated only if I navigate to another screen and navigate back to that screen.

SharedPreferences
import 'package:shared_preferences/shared_preferences.dart';


class UserSimplePreferences {
  static late SharedPreferences _preferences;
  static const _name = 'name';
  static const _age = 'age';
  static const _gender = 'gender';
  static const _church = 'church';
  static const _churchCity = 'churchCity';
  static const _churchState = 'churchState';


  static Future init() async =>
      _preferences = await SharedPreferences.getInstance();

  static Future setName(String name) async =>
      await _preferences.setString(_name, name);

  static String? getName() => _preferences.getString(_name);

  static Future setAge(String age) async =>
      await _preferences.setString(_age, age);

  static String? getAge() => _preferences.getString(_age);

  static Future setGender(String gender) async =>
      await _preferences.setString(_gender, gender);

  static String? getGender() => _preferences.getString(_gender);

  static Future setChurch(String church) async =>
      await _preferences.setString(_gender, church);

  static String? getChurch() => _preferences.getString(_church);


  static Future setChurchCity(String churchCity) async =>
      await _preferences.setString(_gender, churchCity);

  static String? getChurchCity() => _preferences.getString(_churchCity);


  static Future setChurchState(String churchState) async =>
      await _preferences.setString(_gender, churchState);

  static String? getChurchState() => _preferences.getString(_churchState);


}


Register(Here the data should be updated)

class Register extends StatefulWidget {


  @override
  _RegisterState createState() => _RegisterState();
}

class _RegisterState extends State<Register> {
  late String? name = UserSimplePreferences.getName();

 // final user = UserSimplePreferences.getUser();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
     }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
    body:Container(
        child:  Column(
          children: [
            Card(
              child: Text(name??''),
            ),
            Card(
              child: Text(name??''),
            ),
            Card(
              child: Text(name??''),
            )
          ],

       )
      ),
      floatingActionButton: FloatingActionButton(
        backgroundColor: Colors.indigo[900],
        onPressed: () {
          Navigator.push(
            context,
            MaterialPageRoute(builder: (_) {
              return AddUser(idUser: '',);
            }),
          );
        },
        child: Icon(Icons.add, color: Colors.white),
      ),
    );

  }
}



AddUser(where user provides input)
class AddUser extends StatefulWidget {

  final String? idUser;


  const AddUser({required this.idUser});

  @override
  _AddUserState createState() => _AddUserState();

}

class _AddUserState extends State<AddUser> {

  final formKey = GlobalKey<FormState>();
  String name = '';
 String age = '';
 String gender= '';
 String church= '';
 String  churchCity= '';
  String churchState= '';

  @override
  void initState() {
    super.initState();

    // name = UserSimplePreferences.getName() ?? '';
    // age = UserSimplePreferences.getAge() ?? '';
    // gender = UserSimplePreferences.getGender() ?? '';
    // church = UserSimplePreferences.getChurch() ?? '';
    //  churchCity = UserSimplePreferences.getChurchCity() ?? '';
    // churchState = UserSimplePreferences.getChurchState() ?? '';

  }

  @override
  Widget build(BuildContext context) => Scaffold(
    appBar: AppBar(
      automaticallyImplyLeading: false,
      title : Text('Please Enter Your Details',textAlign: TextAlign.center,)),

    body: SafeArea(
          child: ListView(
        padding: EdgeInsets.all(16),
        children: [

          buildName(),
          const SizedBox(height: 12),
          buildAge(),
          const SizedBox(height: 12),
          buildGender(),
          const SizedBox(height: 12),
          buildChurch(),
          const SizedBox(height: 12),
          buildChurchCity(),
          const SizedBox(height: 12),
          buildChurchState(),
          const SizedBox(height: 12),

          buildButton(),
        ],
      ),
    ),
  );

  Widget buildName() =>

    buildTitle(
    title: 'Name',
    child: TextFormField(
      initialValue: name,
      decoration: InputDecoration(
        border: OutlineInputBorder(),
        hintText: 'Your Name',
      ),
      onChanged: (name) => setState(() => this.name = name),
    ),
  );

  Widget buildAge() => buildTitle(
    title: 'Age',
    child: TextFormField(
      initialValue: age,
      decoration: InputDecoration(
        border: OutlineInputBorder(),
        hintText: 'Your Age',
      ),
      onChanged: (age) => setState(() => this.age = age),
    ),
  );
  Widget buildGender() => buildTitle(
    title: 'Gender',
    child: TextFormField(
      initialValue: gender,
      decoration: InputDecoration(
        border: OutlineInputBorder(),
        hintText: 'Your gender(M/F)',
      ),
      onChanged: (gender) => setState(() => this.gender = gender),
    ),
  );
  Widget buildChurch() => buildTitle(
    title: 'Church',
    child: TextFormField(
      initialValue: church,
      decoration: InputDecoration(
        border: OutlineInputBorder(),
        hintText: 'Your church',
      ),
      onChanged: (church) => setState(() => this.church = church),
    ),
  );
  Widget buildChurchCity() => buildTitle(
    title: 'Church City',
    child: TextFormField(
      initialValue: churchCity,
      decoration: InputDecoration(
        border: OutlineInputBorder(),
        hintText: 'Your Church City',
      ),
      onChanged: (churchCity) => setState(() => this.churchCity = churchCity),
    ),
  );
  Widget buildChurchState() => buildTitle(
    title: 'Church State',
    child: TextFormField(
      initialValue: churchState,
      decoration: InputDecoration(
        border: OutlineInputBorder(),
        hintText: 'Your Church State',
      ),
      onChanged: (churchState) => setState(() => this.churchState = churchState),
    ),
  );


  Widget buildButton() => ButtonWidget(
      text: 'Save',
      onClicked: () async {

        await UserSimplePreferences.setName(name);
        await UserSimplePreferences.setAge(age);
        await UserSimplePreferences.setGender(gender);
        await UserSimplePreferences.setChurch(church);
        await UserSimplePreferences.setChurchCity(churchCity);
        await UserSimplePreferences.setChurchState(churchState);
             Navigator.pop(context);
      });

  Widget buildTitle({
    required String title,
    required Widget child,
  }) =>
      Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Text(
            title,
            style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
          ),
          const SizedBox(height: 8),
          child,
        ],
      );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与他有关 2025-02-18 00:53:55

您首次构建视图,然后加载数据。在力setstate上没有视图交互,因此在此视图上没有任何更改。您现在可以做什么?
您有两种方法:

  1. 运行setStatesharone_pref
  2. 使用funerbuilder 以后呈现另一个小部件,然后加载数据后,
    我建议第二选项

You build view first time, then load data. There is no view interaction on force setState so nothing is changed on this view. What do You can do now?
You have 2 ways:

  1. Run setState after load variables from shared_pref
  2. Use FutureBuilder to render another widget before, and after loading data
    I suggest 2nd option
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文