Search code examples
fluttererror-handlingflutter-providerstate-managementriverpod

Too many positional arguments: 0 allowed, but 1 found. Try removing the extra positional arguments


These errors keep appearing and I don't know how to rewrite them to solve the issue .

59:27: Error: Too many positional arguments: 0 allowed, but 1 found. Try removing the extra positional arguments. context.read(exerciseControllerProvider).addExercise(newExercise);

62:27: Error: Too many positional arguments: 0 allowed, but 1 found. Try removing the extra positional arguments. context.read(exerciseControllerProvider).editExercise(updatedExercise);

My code:

import 'package:flutter/material.dart';
import 'package:song/core/models/exercise.dart';
import 'package:song/widgets/exercise_form.dart';
import 'package:song/core/models/exercise_types/front_back.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:song/core/providers/exercise_provider.dart';
import 'package:song/widgets/type_forms/type_controllers.dart';


class ExerciseScreen extends StatefulWidget {
  final Exercise? exercise;

  ExerciseScreen({Key? key, this.exercise}) : super(key: key);

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

class _ExerciseScreenState extends State<ExerciseScreen> {
  GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  TypeControllers? _controllers;

  @override
  void initState() {
    super.initState();
    _controllers = TypeControllers(
      questionController: TextEditingController(text: widget.exercise?.question ?? ''),
      answerController: TextEditingController(text: widget.exercise?.answer ?? ''),
    );
  }

  @override
  void dispose() {
    _controllers?.questionController.dispose();
    _controllers?.answerController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.exercise == null ? 'Create Exercise' : 'Edit Exercise'),
      ),
      body: Padding(
        padding: EdgeInsets.all(16),
        child: ExerciseForm(
          exercise: widget.exercise ?? FrontBack(id: '', front: '', back: ''),
          formKey: _formKey,
          controllers: _controllers!,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          if (_formKey.currentState!.validate()) {
            _formKey.currentState!.save();
            if (widget.exercise == null) {
              Exercise newExercise = FrontBack(id: '', front: _controllers!.questionController.text, back: _controllers!.answerController.text);
              context.read(exerciseControllerProvider).addExercise(newExercise);
            } else {
              Exercise updatedExercise = (widget.exercise as FrontBack).copyWith(front: _controllers!.questionController.text, back: _controllers!.answerController.text);
              context.read(exerciseControllerProvider).editExercise(updatedExercise);
            }
            Navigator.pop(context);
          }
        },
        child: Icon(Icons.check),
      ),
    );
  }
}`

my provider code :

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:song/core/models/exercise.dart';
import 'package:state_notifier/state_notifier.dart';


class ExerciseController extends StateNotifier<List<Exercise>> {
  ExerciseController() : super([]);

  void addExercise(Exercise exercise) {
    state = [...state, exercise];
  }

  void editExercise(Exercise updatedExercise) {
    state = state.map((exercise) => exercise.id == updatedExercise.id ? updatedExercise : exercise).toList();
  }

  void removeExercise(String id) {
    state = state.where((exercise) => exercise.id != id).toList();
  }
}

final exerciseControllerProvider = StateNotifierProvider<ExerciseController, List<Exercise>>(
      (ref) => ExerciseController(),
);
//type_controllers.dart

import 'package:flutter/material.dart';

class TypeControllers {
  TextEditingController questionController;
  TextEditingController answerController;

  TypeControllers({required this.questionController, required this.answerController});
}`

How can I solve it without changing from riverpod ? changed to to ref and to watch listen etc and the same happened


Solution

  • First, you need to use a wrapper over the desired widget Consumer(...) or change the widget itself to ConsumerStatefulWidget. This will add you a ref field available throughout the code.

    Your function calls will look like this:

    ref.read(exerciseControllerProvider.notifier).addExercise(newExercise);
    // and
    ref.read(exerciseControllerProvider.notifier).editExercise(updatedExercise);
    

    Also, your condition may not change if you don't use List.of():

      void removeExercise(String id) {
        final newState = state.where((exercise) => exercise.id != id).toList();
        state = List.of(newState);
      }
    

    Also, the Exercise model must be immutable, with the correct hashcode and ==