我想将一个列表从一个类传递到另一个类,我尝试了不同的方式来做,但是列表却返回空,如何将一个类中的列表传递给另一个类?我需要创建具有该列表的PDF文件,但是当我打印它时,我得到的是[]。
我已经尝试过了:
List<String> getProducts() {
return listaProductos; // This is the list that I'm trying to print in another class, if I call this method in the same class where the method is, I get the filled list, same that I'm trying to pass to another class
}
在我的另一堂课中,我这样做了:
BotanaxState botana2 = new BotanaxState();
List<String> calledList = botana2.getProducts();
print(calledList);
但是它返回一个空列表。
import 'package:dropdown_list/src/pages/firma.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
/****CLASS ONE****/
class Botanax extends StatefulWidget {
@override
BotanaxState createState() => BotanaxState();
}
class BotanaxState extends State<Botanax> {
final List<String> _dropdownValues = [
"One",
"Two",
"Three",
"Four",
"Five"
]; //The list of values we want on the dropdown
String _currentlySelected = ""; //var to hold currently selected value
List<String> listaProductos = new List<String>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Selected Index - DropdownButton'),
),
//display currently selected item on dropdown
body: Container(
alignment: Alignment.center,
child: ListView(
//mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Text(_currentlySelected),
SizedBox(
height: 20.0,
),
DropdownButton(
//value: _currentlySelected,
//map each value from the lIst to our dropdownMenuItem widget
items: _dropdownValues
.map((value) => DropdownMenuItem(
child: Text(value),
value: value,
))
.toList(),
hint: Text('Selecciona producto'),
onChanged: (String value) {
//once dropdown changes, update the state of out currentValue
setState(() {
_currentlySelected = value;
listaProductos.add(_currentlySelected);
});
},
//this wont make dropdown expanded and fill the horizontal space
isExpanded: false,
),
new Column(
children: <Widget>[
SizedBox(
height: 300,
child: ListView.builder(
itemCount: listaProductos.length,
itemBuilder: (BuildContext ctxt, int index) {
return new Center(
child: (new Center(
child: new Text(listaProductos[index]),
)),
);
},
),
),
RaisedButton(
child: Text('Aceptar', style: TextStyle(fontSize: 20)),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => FirmaPage()),
);
//print(getProducts());
},
),
/*RaisedButton(
child: Text('Remove item', style: TextStyle(fontSize: 20)),
onPressed: () {},
)*/
],
)
],
)),
);
}
List<String> getProducts()
{
return listaProductos;
}
/*List<String> MyList
{
get { return myList; }
}*/
List<String> get productos => listaProductos;
}
/****CLASS TWO****/
import 'package:dropdown_list/src/pages/botanax.dart';
import 'package:flutter/material.dart';
import 'package:pdf/widgets.dart' as pdfLib;
import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pdf/pdf.dart';
import 'dart:io';
class FirmaPDF extends StatefulWidget {
@override
_FirmaPDFPageState createState() => new _FirmaPDFPageState();
}
class _FirmaPDFPageState extends State<FirmaPDF> {
List<String> listaProductos = new List<String>();
@override
Widget build(BuildContext context) {
/*listaProductos.add('odwjef');
listaProductos.add('34f');
listaProductos.add('db5');
listaProductos.add('Wolf');
listaProductos.add('fv 4f');
listaProductos.add('Lando');*/
return new Scaffold(
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new RaisedButton(
child: new Text('Print Document'),
onPressed: () => generatePdfAndView(context))
],
),
),
);
}
generatePdfAndView(context) async {
final pdfLib.Document pdf = pdfLib.Document(deflate: zlib.encode);
final String dir = (await getApplicationDocumentsDirectory()).path;
final String path = '$dir/ejemplo.pdf';
final File file = File(path);
BotanaxState botana2 = new BotanaxState();
List<String> calledList = botana2.getProducts();
print(calledList); //Here I'm getting the empty list
//print(calledList);
print(botana2.productos);
//Botanax drop = new Botanax();
//print();
pdf.addPage(pdfLib.Page(
pageFormat: PdfPageFormat.a4,
build: (pdfLib.Context context) {
return pdfLib.Center(
child: pdfLib.ListView(
children: <pdfLib.Widget>[
pdfLib.Text(calledList.toString()),
//pdfLib.Text("Hola"),
//pdfLib.Text("Lando"),
]
)
); // Center
}
)
);
file.writeAsBytesSync(pdf.save());
print(file);
OpenFile.open(path);
}
/*String getTicket(List<String> strings)
{
List<String> list = new List<String>();
for(int i = 0; i < strings.length; i++)
{
list.add(strings[i]);
}
for (var name in list)
{
list.add(strings[name]);
}
return strings.toString();
}*/
}
第二堂课我需要有相同的清单。
答案 0 :(得分:0)
您正在创建new
的 BotanaxState
实例,因此它不包含您在另一个实例中存储的数据是有意义的,即您需要访问包含原始实例的变量。
答案 1 :(得分:0)
您可以将列表传递给第二堂课。
<table>
<tr>
<td>some order things</td>
</tr>
<tr>
<td>some order things</td>
</tr>
<!-- ... -->
<tr t-foreach="o.order_line" t-as="l">
<td t-field="l.product_id.name" />
<!-- ... -->
</tr>
</table>
在第二个类中,在构造函数中声明列表
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => FirmaPage(listaProductos)),
);
然后您可以通过以下方式访问它:
class FirmaPDF extends StatefulWidget {
@override
_FirmaPDFPageState createState() => new _FirmaPDFPageState();
FirmaPDF(this.listaProductos)
List<String> listaProductos;
}