flutter - 我想将变量从一个类传递到另一个类
问题描述
我想将一个类的列表传递给另一个类,我尝试了不同的方法,但我的列表返回为空,我怎样才能将一个类中的列表传递给另一个类?,我需要创建一个 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();
}*/
}
我需要在第二堂课中拥有相同的列表。
解决方案
您可以将列表传递给您的第二个班级。
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => FirmaPage(listaProductos)),
);
在您的第二堂课中,在构造函数中声明列表
class FirmaPDF extends StatefulWidget {
@override
_FirmaPDFPageState createState() => new _FirmaPDFPageState();
FirmaPDF(this.listaProductos)
List<String> listaProductos;
}
然后你可以像下面这样访问它:
...
...
pdf.addPage(pdfLib.Page(
pageFormat: PdfPageFormat.a4,
build: (pdfLib.Context context) {
return pdfLib.Center(
child: pdfLib.ListView(
children: <pdfLib.Widget>[
pdfLib.Text(widget.listaProductos),
//pdfLib.Text("Hola"),
//pdfLib.Text("Lando"),
]
)
); // Center
}
)
);
推荐阅读
- python - 我需要速度。比较两个非常大的字典:键是索引原始熊猫索引存储为元组,值是字符串列表
- php - 如何将多个值的数组从路由传递到laravel 8中的视图
- javascript - 函数内的“Require()”模块不起作用?
- java - 在 Fragment 中使用 ViewModel 和 RecyclerView 时,何时何地是发出 Web 请求的正确位置
- linux - 密码保护 Amazon S3 共享链接,可以吗?
- react-native - 如何在浏览器堆栈中查看日志?
- node.js - 在 MongoDB 中填充 JSON 查询结果
- html - 如何使用 django 变量内联编写 CSS?
- php - 在 Ubuntu 16.04 上安装损坏的 php 和 libssl1.1
- wordpress - VS Code 独立打开的子文件夹显示未定义的函数