flutter_smart_select icon indicating copy to clipboard operation
flutter_smart_select copied to clipboard

Choice value always displays null when loading page first

Open FLAGLORD opened this issue 5 years ago • 2 comments

i expect that the init value would be from SharedPreferences,so i invoke initSoundSetting,but when loading page ,it always displays null

class _StudyModelSoundSettingState extends State<StudyModelSoundSetting>{
  
  int soundSetting;
  void initState(){
    super.initState();
    initSoundSetting();
  }
  
  void initSoundSetting()async{
    final SharedPreferences prefs = await widget.sp;
    final soundSettingValue = prefs.getInt('soundSetting');
    this.setState(() {
      this.soundSetting = soundSettingValue==null?4:soundSettingValue;
    });
  }
  
  void changeSoundSetting(int newValue)async{
    final SharedPreferences prefs = await widget.sp;
    setState(() {
      this.soundSetting = newValue;
    });
    prefs.setInt('soundSetting', newValue);
  }
  
  List<S2Choice<int>> soundSettingOptions=[
    S2Choice<int>(value:1,title:'正常'),
    S2Choice<int>(value:2,title:'震动'),
    S2Choice<int>(value:3,title:'静音'),
    S2Choice<int>(value:4,title:'系统预设'),
  ];
  
  Widget build(BuildContext context){
    print(this.soundSetting);
    return SmartSelect<int>.single(
        title:'铃声模式',
        choiceItems:soundSettingOptions,
        modalType: S2ModalType.bottomSheet,
        value: this.soundSetting,
        onChange: (state){
          changeSoundSetting(state.value);
        },
    );
  }
}

FLAGLORD avatar Oct 31 '20 10:10 FLAGLORD

I released https://pub.dev/packages/flutter_awesome_select with fixed null safety. Could you check that everything works for you?

vasilich6107 avatar Nov 12 '21 23:11 vasilich6107

i expect that the init value would be from SharedPreferences,so i invoke initSoundSetting,but when loading page ,it always displays null

class _StudyModelSoundSettingState extends State<StudyModelSoundSetting>{
  
  int soundSetting;
  void initState(){
    super.initState();
    initSoundSetting();
  }
  
  void initSoundSetting()async{
    final SharedPreferences prefs = await widget.sp;
    final soundSettingValue = prefs.getInt('soundSetting');
    this.setState(() {
      this.soundSetting = soundSettingValue==null?4:soundSettingValue;
    });
  }
  
  void changeSoundSetting(int newValue)async{
    final SharedPreferences prefs = await widget.sp;
    setState(() {
      this.soundSetting = newValue;
    });
    prefs.setInt('soundSetting', newValue);
  }
  
  List<S2Choice<int>> soundSettingOptions=[
    S2Choice<int>(value:1,title:'正常'),
    S2Choice<int>(value:2,title:'震动'),
    S2Choice<int>(value:3,title:'静音'),
    S2Choice<int>(value:4,title:'系统预设'),
  ];
  
  Widget build(BuildContext context){
    print(this.soundSetting);
    return SmartSelect<int>.single(
        title:'铃声模式',
        choiceItems:soundSettingOptions,
        modalType: S2ModalType.bottomSheet,
        value: this.soundSetting,
        onChange: (state){
          changeSoundSetting(state.value);
        },
    );
  }
}

Same problem here, any solution?

spalenzuela avatar Aug 16 '22 14:08 spalenzuela

Hi everyone,

I'm really sorry for not maintaining the smart_select package in a long time. It's been a great project, but it's become too difficult for me to maintain, especially since I've been going through a tough time for the past few years.

In its place, I've released a new package called choice. The combination to smart_select and chips_choice with cleaner, more flexible, and composable API for creating inline or prompted choice widgets with single or multiple selection.

I hope you'll check out choice. I think you'll find it to be a great replacement for smart_select.

Thanks for your understanding.

davigmacode avatar Aug 26 '23 11:08 davigmacode