Laurent Thomas
Laurent Thomas

Reputation: 282

Too many positional arguments: 0 expected, but 2 found. & dead code

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



Upvotes: 0

Views: 254

Answers (1)

Kaushik Chandru
Kaushik Chandru

Reputation: 17762

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

Upvotes: 1

Related Questions