1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Flutter Riverpod 2.5.1 not updating StateNotifier value on read

Discussão em 'Mobile' iniciado por Stack, Outubro 9, 2024 às 20:42.

  1. Stack

    Stack Membro Participativo

    I have a flutter app and I want to use flutter_riverpod as a global state manager. The problem is that I cant access my variable even when i use the read method. What am I doing wrong?

    My Screen code:


    class MySCcreenScreen extends ConsumerStatefulWidget {
    const MySCcreenScreen({super.key});
    @override
    ConsumerState<MySCcreenScreen> createState() =>
    _MySCcreenScreenState();
    }

    class _MySCcreenScreenState
    extends ConsumerState<MySCcreenScreen>
    with AutomaticKeepAliveClientMixin {
    bool disabled = true;
    @override
    Widget build(BuildContext context) {
    super.build(context);
    final form = ref.watch(complimentFormProvider);
    safePrint(form);

    void handleProceed() {
    safePrint('form.id ${form.id}');
    }

    double baseScreenPadding = 20;
    double screenHeight = getScreenHeight(context);

    return Scaffold(
    padding: EdgeInsets.symmetric(horizontal: baseScreenPadding),
    considerScroll: true,
    body:
    ConstrainedBox(
    constraints: BoxConstraints(
    minHeight: screenHeight,
    ),
    child: Column(
    children: [
    MyInput(
    handleSubmit: handleProceed,
    ),
    const SizedBox(height: 20),
    SubmitButton(
    text: 'Submit',
    disabled: disabled,
    onPress: handleProceed,
    ),
    ],
    ))
    );
    }

    @override
    bool get wantKeepAlive => true;
    }


    MyInput code

    class MyInput extends ConsumerWidget {
    final Function() handleSubmit;

    const MyInput({super.key, required this.handleSubmit});

    @override
    Widget build(BuildContext context, WidgetRef ref) {
    final form = ref.watch(formProvider);

    void setId(String newValue) {
    safePrint('newValue $newValue');
    safePrint('form.id ${form.id}');
    ref
    .read(formProvider.notifier)
    .setId(newValue);
    }

    return Column(
    children: [
    const SizedBox(height: 20),
    InputId(
    setId: setId,
    onSubmit: handleSubmit,
    keyboardAction: TextInputAction.next,
    ),
    ],
    );
    }
    }


    My InputId code:

    void emptyFunction() {}

    class InputId extends StatefulWidget {
    final ValueChanged<String> setId;
    final TextInputAction keyboardAction;
    final Function() onSubmit;

    const InputId({
    super.key,
    required this.setId,
    this.keyboardAction = TextInputAction.done,
    this.onSubmit = emptyFunction,
    });

    @override
    State<InputId> createState() => _InputIdState();
    }

    class _InputIdState extends State<InputId> {
    final TextEditingController inputController = TextEditingController();

    @override
    void dispose() {
    inputController.dispose();
    super.dispose();
    }

    void updateId() {
    widget.setId(inputController.text);
    }

    @override
    Widget build(BuildContext context) {
    return Column(
    mainAxisAlignment: MainAxisAlignment.center,
    children: <Widget>[
    TextField(
    textInputAction: widget.keyboardAction,
    onEditingComplete: widget.onSubmit,
    controller: inputController,
    enableSuggestions: false,
    autocorrect: false,
    keyboardType: TextInputType.number,
    enableInteractiveSelection: false,
    autofocus: true,
    onChanged: (value) {
    updateId();
    },
    ),
    ],
    );
    }
    }


    and my riverpod code:

    class MyFormState {
    String id;
    String name;

    MyFormState(
    {this.id = '', this.name = ''});
    }

    class MyFormNotifier extends StateNotifier<MyFormState> {
    MyFormState _state = MyFormState();

    MyFormNotifier() : super(MyFormState());

    void setId(String id) {
    _state = MyFormState(
    id: id,
    name: _state.name,
    );
    }

    void setName(String name) {
    _state = MyFormState(
    id: _state.id,
    name: name,
    );
    }
    }

    final formProvider =
    StateNotifierProvider<MyFormNotifier, MyFormState>(
    (ref) => MyFormNotifier(),
    );



    It is important to note, that I am suspicious about the tree widget: screen ---Input ------InputId Does this has any influence?

    Continue reading...

Compartilhe esta Página