0

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();
  }
}

I have modified the code and started from scratch again. Below you will find the code.


import 'package:flutter_swipe_action_cell/core/cell.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/foundation.dart';
import 'package:multi_select_flutter/bottom_sheet/multi_select_bottom_sheet_field.dart';
import 'package:multi_select_flutter/util/multi_select_item.dart';

String inboxTaskDisplayed='';
int nbRecord=0;
var taskSelectedID;
var taskDone;

List <dynamic> curent_Context=[];
List<dynamic> allContexts=[];
List<Animal> _selectedAnimals2 = [];

//------TEST
List allMyContext=[];


class Animal {
  final int id;
  final String name;

  Animal({
    this.id,
    this.name,
  });
}

class ContextExisting {
  final int id;
  final String name;

  ContextExisting({
    this.id,
    this.name,
  });
}

TextEditingController passController = new TextEditingController();
//-----------------
var documentID;
var textController = TextEditingController();
var popUpTextController = TextEditingController();

 List<Animal> _animals = [
Animal(id: 1, name: "Lion"),
Animal(id: 2, name: "Flamingo"),
Animal(id: 3, name: "Hippo"),];
 
 List<ContextExisting> _contexts = [];


//_-----------------
class Inbox_Test_For_Chip_Trial extends StatefulWidget {
  final String screenSelected;
  final String titlePage;
  Inbox_Test_For_Chip_Trial(Key key, {@required this.screenSelected, @required this.titlePage,}) : super(key: key);
  @override
  _Inbox_Test_For_Chip_TrialState createState() => _Inbox_Test_For_Chip_TrialState(screenSelected, titlePage);
}

class _Inbox_Test_For_Chip_TrialState extends State<Inbox_Test_For_Chip_Trial> {
  GlobalKey<FormState> _inboxFormKey = GlobalKey<FormState>();
  String screenSelected;
  String titlePage;

  _Inbox_Test_For_Chip_TrialState(this.screenSelected,  this.titlePage,);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: new AppBar(
        title: new Text(titlePage + ' ('+nbRecord.toString()+')'),
    actions: <Widget>[

    ],
    ),
    
    backgroundColor: Colors.white,
      body: Container(
        height: 250,
        child: Column(
          //mainAxisAlignment: MainAxisAlignment.center,
          children: [
            //FOR CONTEXT
            Flexible(child: StreamBuilder(
            stream: FirebaseFirestore.instance
          .collection('Users')
          .doc(FirebaseAuth.instance.currentUser.uid)
          .collection('contexts')
          .snapshots(),
                    builder: (BuildContext context,
                    AsyncSnapshot<QuerySnapshot> snapshot) {
                    if (!snapshot.hasData) {
                    return Center(
                    child: CircularProgressIndicator(),
                    );
                    }
                    else {
                return Container(
                height: MediaQuery.of(context).size.height * .78,
                width: MediaQuery.of(context).size.width,
                child: ListView(
                children: snapshot.data.docs.map((document) {
                return Wrap(
                children: [Card(
                child: SwipeActionCell(
                key: ObjectKey(document['context_Name']),
                trailingActions: <SwipeAction>[

          ],
      
          
          child: ListTile(
          trailing: IconButton(
          icon: Icon(Icons.keyboard_arrow_right),
          onPressed: () async {
          taskSelectedID = FirebaseFirestore
                .instance
                .collection('Users')
                .doc(
          FirebaseAuth.instance.currentUser
                .uid)
                .collection('contexts')
                .doc(document.id).toString();
      
          final doc =  FirebaseFirestore.instance
                .collection('users')
                .doc('contexts')
                .get();
          
        for (int i=0, i<doc.length, i++) {
          List <allMyContext> = ContextExisting (id:i, name:doc.name)
        };
      
    }
    ),

    leading: ConstrainedBox(
    constraints: BoxConstraints(
    minWidth: 30,
    minHeight: 35,
    maxWidth: 30,
    maxHeight: 35,
    ),

     //InkWell(child: Icon(Icons.check_box_outline_blank),
    ),
    title: Text(

    document['context_Name'],
    maxLines: 2,
    overflow: TextOverflow.ellipsis,
    ),
    ),
    ),
    ),
    ]
    );
    }).toList(),
    ),
    );
    }
    }),),

            Container(child: TESTWIDGET())

          ],
        ),
      ),
      //bottomNavigationBar:  MyBottomAppBar(),  //PersistentBottomNavBar(),
    );
  }
}

class TESTWIDGET extends StatefulWidget {

  final _items = _animals
      .map((animal) => MultiSelectItem<Animal>(animal, animal.name))
      .toList();

  TESTWIDGET({Key key})  : super(key: key);

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

class _TESTWIDGETState extends State<TESTWIDGET> {
  List<Animal> _selectedAnimals2 = [];
  List items;
  List<ContextExisting> selectedContext = [];

  final _items = _animals
      .map((animal) => MultiSelectItem<Animal>(animal, animal.name))
      .toList();
  @override
  Widget build(BuildContext context) {

    return Column(
      children: [

        MultiSelectBottomSheetField(
          buttonText: Text("Contexts"),
          onConfirm: (val2) {
            // _selectedAnimals5 = val2;
          },

          items: _items,
          initialValue:
          _selectedAnimals2, 
        ),
      ],
    );
  }
}


class TestWidgetContext extends StatefulWidget {

  
  final _items = _Contexts
      .map((Contexts) => MultiSelectItem<Animal>(ContextExisting, ContextExisting.name))
      .toList();

  TestWidgetContext({Key key})  : super(key: key);

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

class _TestWidgetContextState extends State<TestWidgetContext> {
  List itemsContext;
  List<ContextExisting> selectedContext = [];

  final _itemsContext = _contexts
      .map((context) => MultiSelectItem<Animal>(ContextExisting, context.name))
      .toList();
  @override
  Widget build(BuildContext context) {

    return Column(
      children: [

        MultiSelectBottomSheetField(
          buttonText: Text("Contexts"),
          onConfirm: (val2) {
            // _selectedAnimals5 = val2;
          },

          items: _itemsContext,
          initialValue:
          _selectedAnimals2,
        ),
      ],
    );
  }
}



2
  • Which flutter version are you using? Commented Jun 11, 2022 at 8:41
  • I am using the version 2.2.3 Commented Jun 11, 2022 at 8:45

1 Answer 1

1

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()
      ],
    );
  }
}

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you. To give you more details about what I am trying to achieve. In an other View, I am collecting information/fields from Firebase document. One of the field is a an array. I am willing to pass to TestWidget, a List containing the value, so I can see them in MultiSelectBottomSheetField. Then, the chip selected will be recorded in Firebase document. I hope it is clear. Please, can you confirm that with the code I will be able to do that. Many thanks.
it solely depends on how you write the codes. This widget can be used to display a list of items if you use a List.generate or Listview.builder
I must go out now, but when I will be back home, I will share more code. Many thanks for your help.
I have added some context to explain what I am trying to achieve. Many thanks for your help.
Is it still giving you errors?\
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.