Home > Software engineering >  CheckboxListTile - too many positional arguments
CheckboxListTile - too many positional arguments

Time:11-25

I'm trying to add a CheckboxListTile and found the checkbox code from flutter dev and tried to implement it in my widget shown below. But I get errors

Error: too many positional arguments, positional arguments must occur before named arguments

    import 'package:flutter/material.dart';

     @override
  Widget build(BuildContext context) {
     bool _checked = true;
    return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
                    
                  Container(
                    
                    width: widget.sentenceWidth,
                    child: CheckboxListTile(
                      title: Text('me, the label'),
                      controlAffinity: ListTileControlAffinity.leading,
                     value: _checked ,
                      onChanged: (bool value) setState(() {
                      _checked =  value;
                      }
                  ),),),

CodePudding user response:

Here is the working code:

You have passed bool as type of variable in onChange. I have also changed width param so also check for that param might be passed wrongly.

  @override
  Widget build(BuildContext context) {
    bool _checked = true;
    return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          SizedBox(
              width: 500,
              child: CheckboxListTile(
                title: const Text('me, the label'),
                controlAffinity: ListTileControlAffinity.leading,
                value: _checked,
                onChanged: (value) {
                  print(value);
                },
              ))
        ]);
  }

Try this and do let me know if you get any problem.

CodePudding user response:

try out this and declare your _checked outside buildcontext

 bool _checked = true;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('TabBar Widget'),
      ),
      body: Container(
        width: double.infinity,
        child: CheckboxListTile(
            title: Text('me, the label'),
            controlAffinity: ListTileControlAffinity.leading,
            value: _checked,
            onChanged: (value) {
              setState(() {
                _checked = value;
              });
            }),
      )

Complete source code

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

/// This is the main application widget.
class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: _title,
      home: MyStatefulWidget(),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key key}) : super(key: key);

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  bool _checked = true;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('TabBar Widget'),
      ),
      body: Container(
        width: double.infinity,
        child: CheckboxListTile(
            title: Text('me, the label'),
            controlAffinity: ListTileControlAffinity.leading,
            value: _checked,
            onChanged: (value) {
              setState(() {
                _checked = value;
              });
            }),
      ),
    );
  }
}
  • Related