gpt4 book ai didi

flutter - Flutter-Navigator.push值列出

转载 作者:行者123 更新时间:2023-12-03 04:35:44 25 4
gpt4 key购买 nike

我在小部件中有一个Navigator.push,可将值解析为新的小部件。可以了在我解析值的有状态小部件中,我试图将值添加到列表中,但是在这里出现错误:
Producerlist.add(生产者)
任何想法如何解决这个问题?
Navigator.push:

void _sendDataToSecondScreen(BuildContext context) {
String textToSend = textFieldController.text;
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Test2(producer: textToSend,),
));
}
}
Test2.dart:
import 'package:flutter/material.dart';

class Test2 extends StatefulWidget {
final String producer;
List<String> Producerlist = [];

Test2({Key key, @required this.producer}) : super(key: key);

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

class _Test2State extends State<Test2> {
List<String> Producerlist = [];
Producerlist.add (producer)

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Test2')),
body: ListView.builder(
itemCount: Producerlist.length,
itemBuilder: (context, index){
return Card(
child: ListTile(
onTap: (){},
title: Text(Producerlist[index]),
),
);
},
),
);
}
}

最佳答案

您可以将producer传递给Test2State,或者(我相信,自己还没有尝试过,但是看到过类似的东西),您可以使用Producerlist.add (widget.producer)更新:
正如我说的,我自己还没有使用widget.尝试过此操作。因此,要确定,请尝试

import 'package:flutter/material.dart';

class Test2 extends StatefulWidget {
final String producer;

Test2({Key key, @required this.producer}) : super(key: key);

@override
_Test2State createState() => _Test2State(producer);
}

class _Test2State extends State<Test2> {
_Test2State(this.producer);
final String producer;
List<String> Producerlist = [];
Producerlist.add (producer)

关于flutter - Flutter-Navigator.push值列出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64163257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com