在Flutter中将列表传递给另一个班级

6jjcrrmo  于 2022-12-24  发布在  Flutter
关注(0)|答案(3)|浏览(146)

我在NameViewList中有一个字符串列表“names”,并试图将其值传递给另一个类Basic()。
我尝试过这种方法,但它给了我一个错误:错误:“names”是final,并且在声明时已给定值,因此无法将其设置为新值。([flutterspinningwheel]库/名称视图列表.dart中的final_initialized_in_declaration_and_constructor:5)
我用这种方法传递变量,它是工作的,但当我传递列表,它不工作

class NameViewList extends StatefulWidget {
  NameViewList({Key key, this.names});
  final List<String> names = <String>[];

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

class _NameViewListState extends State<NameViewList> {

  TextEditingController nameController = TextEditingController();

   createDialog(BuildContext context){
    return showDialog(context: context, builder: (context) {
      return AlertDialog(
        title: Text('Insert Name'),
        content: TextField(
          controller: nameController,
          decoration: InputDecoration(
            border: OutlineInputBorder(),
            labelText: 'Contact Name',
          ),
        ),
        actions: [
          MaterialButton(
            child: Text('Submit'),
            onPressed: (){
              addItemToList();
              Navigator.of(context).pop();
            },
          )
        ],
      );
    });
  }

  void addItemToList(){
    setState(() {
      widget.names.insert(0, nameController.text);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Settings'),
        centerTitle: true,
      ),
      body: Column(
        children: [
          IconButton(
            icon: Icon(Icons.queue, color: Colors.green,),
            onPressed: (){createDialog(context);},
          ),
          Padding(
            padding: EdgeInsets.all(20),
            child: TextField(
              controller: nameController,
              decoration: InputDecoration(
                border: OutlineInputBorder(),
                labelText: 'Contact Name',
              ),
            ),
          ),
          Row(
            children: [
              RaisedButton(
                child: Text('Clear'),
                onPressed: () {
                  setState(() {
                    widget.names.clear();

                  });
                },
              ),
              RaisedButton(
                child: Text('Submit'),
                onPressed: () {
                  Navigator.of(context).push(MaterialPageRoute(builder: (context)=> Basic(names: names)));
                },
              ),
            ],
          ),
qv7cva1a

qv7cva1a1#

更改此行:

NameViewList({Key key, this.names});
  final List<String> names = <String>[];

收件人:

NameViewList({List<String> names}) : this.names = names ?? [];
  final List<String> names;

你的问题是你给一个final变量设置了两次值。首先你把它设置为一个空的字符串列表。然后你再次设置它,如果可选参数(如果没有可选参数,值被设置为null)。
相反,我的解决方案改变了它,所以我们只设置names变量一次,首先询问可选参数是否为null(未设置),如果是这种情况,我们将值设置为空列表,否则,我们使用可选参数。

ct3nt3jp

ct3nt3jp2#

如果变量是用final构造函数声明的,你就不能修改它。所以,你可以创建另一个变量,并将final的值赋给它。这个问题可以这样解决:

class NameViewList extends StatefulWidget {
  final List<String> names;
  NameViewList({Key key, this.names});

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

class _NameViewListState extends State<NameViewList> {
  TextEditingController nameController = TextEditingController();
  List<String> names = <String>[];

  createDialog(BuildContext context) {
    return showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text('Insert Name'),
            content: TextField(
              controller: nameController,
              decoration: InputDecoration(
                border: OutlineInputBorder(),
                labelText: 'Contact Name',
              ),
            ),
            actions: [
              MaterialButton(
                child: Text('Submit'),
                onPressed: () {
                  addItemToList();
                  Navigator.of(context).pop();
                },
              )
            ],
          );
        });
  }

  void addItemToList() {
    setState(() {
      names.insert(0, nameController.text);
    });
  }

  @override
  void initState() {
    super.initState();
    names = widget.names;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Settings'),
        centerTitle: true,
      ),
      body: Column(
        children: [
          IconButton(
            icon: Icon(
              Icons.queue,
              color: Colors.green,
            ),
            onPressed: () {
              createDialog(context);
            },
          ),
          Padding(
            padding: EdgeInsets.all(20),
            child: TextField(
              controller: nameController,
              decoration: InputDecoration(
                border: OutlineInputBorder(),
                labelText: 'Contact Name',
              ),
            ),
          ),
          Row(
            children: [
              RaisedButton(
                child: Text('Clear'),
                onPressed: () {
                  setState(() {
                    names.clear();
                  });
                },
              ),
              RaisedButton(
                child: Text('Submit'),
                onPressed: () {
                  Navigator.of(context).push(MaterialPageRoute(
                      builder: (context) => Basic(names: names)));
                },
              ),
            ],
          )
        ],
      ),
    );
  }
}
pw136qt2

pw136qt23#

我建议使列表可以为空,然后在实际使用列表的地方检查是否为空。

NameViewList({super.key,this.names});
final List<String>? names;

相关问题