Home > Software engineering >  Too many positional arguments: 0 expected, but 2 found. & dead code
Too many positional arguments: 0 expected, but 2 found. & dead code

Time:06-11

I am getting an error in the code below 'Too many positional arguments: 0 expected, but 2 found.', but I do not understand why. I have declared two list. Also, I am having dead code starting at the line selectedContext == null || selectedContext.isEmpty

I have remove comma, but it does not changed the problem. If you can point me into the right direction, it would be appreciated. Many thanks in advance.

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:multi_select_flutter/multi_select_flutter.dart';

class TESTWIDGET extends StatefulWidget {
  final List items;
  final List selectedContext;

  TESTWIDGET({Key key,@required this.items,@required this.selectedContext})  : super(key: key);

  @override
  _TESTWIDGETState createState() => _TESTWIDGETState(items,selectedContext);
}

class _TESTWIDGETState extends State<TESTWIDGET> {
  List items,selectedContext;

@override
  Widget build(BuildContext context) {

    return MultiSelectBottomSheetField(
      initialChildSize: 0.4,
      listType: MultiSelectListType.CHIP,
      searchable: true,
      buttonText: Text("Favorite Animals"),
      title: Text("Animals"),
      items: items,
      onConfirm: (values) {
        selectedContext = values;
      },
      chipDisplay: MultiSelectChipDisplay(
        onTap: (value) {
          setState(() {
            selectedContext.remove(value);
          });
        },
      ),
    );
    selectedContext == null || selectedContext.isEmpty
        ? Container(
        padding: EdgeInsets.all(10),
        alignment: Alignment.centerLeft,
        child: Text(
          "None selected",
          style: TextStyle(color: Colors.black54),
        ))
        : Container();
  }
}

CodePudding user response:

You are returning MultiSelectBottomSheetField so the container below that became dead code. Wrap those widgets with columns. Also you don't have to pass the arguments again to the state, you can instead use widget.varName

class TestWidget extends StatefulWidget {
  final List<MultiSelectItem<Object?>> items;
  late final List selectedContext;

  TestWidget({required this.items, required this.selectedContext});

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

class _TestWidgetState extends State<TestWidget> {
  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        MultiSelectBottomSheetField(
          initialChildSize: 0.4,
          listType: MultiSelectListType.CHIP,
          searchable: true,
          buttonText: Text("Favorite Animals"),
          title: Text("Animals"),
          items: widget.items,
          onConfirm: (values) {
            widget.selectedContext = values;
          },
          chipDisplay: MultiSelectChipDisplay(
            onTap: (value) {
              setState(() {
                widget.selectedContext.remove(value);
              });
            },
          ),
        ),
        widget.selectedContext.isEmpty
            ? Container(
                padding: EdgeInsets.all(10),
                alignment: Alignment.centerLeft,
                child: Text(
                  "None selected",
                  style: TextStyle(color: Colors.black54),
                ))
            : Container()
      ],
    );
  }
}

  • Related