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

huangapple go评论61阅读模式
英文:

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

问题

以下是您要翻译的部分:

> 59:27: 错误:位置参数太多:允许 0 个,但找到了 1 个。
> 请删除额外的位置参数。
> context.read(exerciseControllerProvider).addExercise(newExercise);
> 
> 62:27: 错误:位置参数太多:允许 0 个,但找到了 1 个。
> 请删除额外的位置参数。
> context.read(exerciseControllerProvider).editExercise(updatedExercise);

对于这些错误,您可以尝试将代码从使用context.read更改为使用ref,并使用ref.watchref.listen等方法。这可能需要对您的提供程序和状态管理逻辑进行一些更改,以适应Riverpod的更新。

请注意,这只是一个概括性的建议,具体的更改可能会根据您的代码结构和逻辑而有所不同。如果需要更多帮助,请提供更多有关您的代码和问题的详细信息。

英文:

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

答案1

得分: 2

首先,您需要在所需的小部件上使用一个包装器 Consumer(...),或者将小部件本身更改为 ConsumerStatefulWidget。这将为您添加一个在整个代码中可用的 ref 字段。

您的函数调用将如下所示:

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

此外,如果不使用 List.of(),您的条件可能不会改变:

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

此外,Exercise 模型必须是不可变的,具有正确的 hashcode==

英文:

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 ==

huangapple
  • 本文由 发表于 2023年4月13日 14:47:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定