Flutter 如何在flatter中单独从数组中获取值?
我试图获得以下条形码的信息使用“获取按钮” 首先,我扫描条形码,然后把它放在一个列表上。用这个Flutter 如何在flatter中单独从数组中获取值?,flutter,Flutter,我试图获得以下条形码的信息使用“获取按钮” 首先,我扫描条形码,然后把它放在一个列表上。用这个 Future _scanQR() async { try { String barcode = await BarcodeScanner.scan(); setState(() { this.barcode = barcode; list.insert( 0, barcode, );
Future _scanQR() async {
try {
String barcode = await BarcodeScanner.scan();
setState(() {
this.barcode = barcode;
list.insert(
0,
barcode,
);
});
} on PlatformException catch (ex) {
if (ex.code == BarcodeScanner.CameraAccessDenied) {
setState(() {
this.barcode = 'The user did not grant the camera permission!';
});
} else {
setState(() {
this.barcode = "Unknown Error $ex";
});
}
} on FormatException {
setState(() {
this.barcode = "You pressed the back button before scanning anything";
});
} catch (ex) {
setState(() {
this.barcode = "Unknown Error $ex";
});
}
}
然后,使用以下命令打印列表:
Expanded(
child: ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: list.length,
itemBuilder: (BuildContext context, int index) {
return Container(
height: 50.0,
margin: EdgeInsets.all(2),
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text('${list[index]}'),
TextButton(
onPressed: () {
getInfo();
},
child: Text("Get Info"),
),
],
扩展(
子项:ListView.builder(
填充:常量边集。全部(8),
itemCount:list.length,
itemBuilder:(构建上下文,int索引){
返回容器(
身高:50.0,
保证金:所有(2),
儿童:中心(
孩子:排(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
文本(“${list[index]}”),
文本按钮(
已按下:(){
getInfo();
},
子项:文本(“获取信息”),
),
],
请参见此处,列表是在每个结果中生成的,其中包含“获取信息”。
例如,我扫描了两个条形码。4912548796616和8998666001719,因此它们被插入到“列表”列表中。当我打印“列表”时,我的输出是:i/flatter(16927):[4912548796616,8998666001719],这意味着我们的列表创建工作正常,现在我的问题是,如何分别获取它们?我尝试创建了一个“getInfo()”但它只显示我扫描的最新条形码。我希望你能帮助我。谢谢
这是我的getInfo函数,我正在准备查询,以获取附加到条形码的完整信息。这是一个不起作用的函数,因为它只打印扫描的最新条形码
Future<List> getInfo() async {
print(this.barcode);
}
Future getInfo()异步{
打印(此为条形码);
}
谢谢克雷格的回答……所以我改变了答案
Future<List> getInfo() async {
print(this.barcode);
}
Future getInfo()异步{
打印(此为条形码);
}
对此
Future<List> getInfo(index) async {
print(list[index]);
}
Future getInfo(索引)异步{
打印(列表[索引]);
}
您只需要引用列表中的单个项目。您可以使用“for”循环遍历它们……例如for(var i=0;i能给我一个示例吗?我有点迷路了。