[英]Convert List<List<dynamic>> to ArrayList in Flutter
I'm new to flutter and i want to convert a List<List> to ArrayList.我是 flutter 的新手,我想将 List<List> 转换为 ArrayList。 here's my current list output这是我当前的列表 output
data [[Qty, Desc, Unitprice, totalprice], [1, Jeans, 106.0, 106.0], [2, Shirt, 24.38, 24.38], [3, Demo Hoodie / XL, 106.0, 318.0], [4, Demo Hoodie / 2XL, 106.0, 106.0], [1, Jeans-short, 106.0, 106.0], [5, Shirt-Long, 24.38, 24.38], [6, Demo Hoodie-Lg / XL, 106.0, 318.0], [4, Demo Hoodie-Mg / 2XL, 106.0, 106.0], [3, Jeans-RG, 106.0, 106.0], [4, Shirt-MG, 24.38, 24.38], [7, Demo Hoodie-TY / XL, 106.0, 318.0], [9, Demo Hoodie-Levis / 2XL, 106.0, 106.0], [1, Demo Hoodie-J&J / XL, 106.0, 318.0], [2, Demo Hoodie-M&J / 2XL, 106.0, 106.0], [7, Temo Hoodie-TY / XL, 106.0, 318.0], [5, Zemo Hoodie-Levis / 2XL, 106.0, 106.0], [6, Aemo Hoodie-J&J / XL, 106.0, 318.0], [2, Bemo Hoodie-M&J / 2XL, 106.0, 106.0], [9, Temo Hoodie-TY / XL, 106.0, 318.0], [1, Zemo Hoodie-Levis / 2XL, 106.0, 106.0], [2, Aemo Hoodie-J&J / XL, 106.0, 318.0], [7, Bemo Hoodie-M&J / 2XL, 106.0, 106.0]]数据 [[数量, Desc, 单价, 总价], [1, 牛仔裤, 106.0, 106.0], [2, 衬衫, 24.38, 24.38], [3, Demo Hoodie / XL, 106.0, 318.0], [4, Demo Hoodie / 2XL, 106.0, 106.0], [1, 牛仔裤-短裤, 106.0, 106.0], [5, 衬衫-长, 24.38, 24.38], [6, Demo Hoodie-Lg / XL, 106.0, 318.0], [4, Demo Hoodie-Mg / 2XL, 106.0, 106.0], [3, Jeans-RG, 106.0, 106.0], [4, Shirt-MG, 24.38, 24.38], [7, Demo Hoodie-TY / XL, 106.0, 318.0] , [9, Demo Hoodie-Levis / 2XL, 106.0, 106.0], [1, Demo Hoodie-J&J / XL, 106.0, 318.0], [2, Demo Hoodie-M&J / 2XL, 106.0, 106.0], [7, Temo Hoodie-TY / XL, 106.0, 318.0], [5, Zemo Hoodie-Levis / 2XL, 106.0, 106.0], [6, Aemo Hoodie-J&J / XL, 106.0, 318.0], [2, Bemo Hoodie-M&J / 2XL , 106.0, 106.0], [9, Temo Hoodie-TY / XL, 106.0, 318.0], [1, Zemo Hoodie-Levis / 2XL, 106.0, 106.0], [2, Aemo Hoodie-J&J / XL, 106.0, 318.0] , [7, Bemo Hoodie-M&J / 2XL, 106.0, 106.0]]
I want to add that array list into recyclerView in flutter.我想将该数组列表添加到 flutter 的 recyclerView 中。
Thanks in advance.提前致谢。
You can either use dart:convert
and use jsonDecode
but that won't work unless you remove strings from this for add single/double quotes to each word.您可以使用dart:convert
并使用jsonDecode
但这将不起作用,除非您从中删除字符串以便为每个单词添加单引号/双引号。
Or you could convert it manually by splitting the string.或者您可以通过拆分字符串来手动转换它。
import 'dart:core';
main(){
String data = "[[Qty, Desc, Unitprice, totalprice], [1, Jeans, 106.0, 106.0], [2, Shirt, 24.38, 24.38], [3, Demo Hoodie / XL, 106.0, 318.0], [4, Demo Hoodie / 2XL, 106.0, 106.0], [1, Jeans-short, 106.0, 106.0], [5, Shirt-Long, 24.38, 24.38], [6, Demo Hoodie-Lg / XL, 106.0, 318.0], [4, Demo Hoodie-Mg / 2XL, 106.0, 106.0], [3, Jeans-RG, 106.0, 106.0], [4, Shirt-MG, 24.38, 24.38], [7, Demo Hoodie-TY / XL, 106.0, 318.0], [9, Demo Hoodie-Levis / 2XL, 106.0, 106.0], [1, Demo Hoodie-J&J / XL, 106.0, 318.0], [2, Demo Hoodie-M&J / 2XL, 106.0, 106.0], [7, Temo Hoodie-TY / XL, 106.0, 318.0], [5, Zemo Hoodie-Levis / 2XL, 106.0, 106.0], [6, Aemo Hoodie-J&J / XL, 106.0, 318.0], [2, Bemo Hoodie-M&J / 2XL, 106.0, 106.0], [9, Temo Hoodie-TY / XL, 106.0, 318.0], [1, Zemo Hoodie-Levis / 2XL, 106.0, 106.0], [2, Aemo Hoodie-J&J / XL, 106.0, 318.0], [7, Bemo Hoodie-M&J / 2XL, 106.0, 106.0]]";
List<String> splittedData = data.substring(2,data.length-1).split("], [");
List<List<String>> formattedData = [];
splittedData.forEach( (stringVal) => formattedData.add(stringVal.split(', ')));
print(formattedData[0][0]);
}
You run this code dartpad.dev to check its output.您运行此代码dartpad.dev以检查其 output。
You improve this further by using regular expressions to parse the data instead of splitting it using a string.您可以通过使用正则表达式来解析数据而不是使用字符串拆分数据来进一步改进这一点。 That would be the case where the data itself isn't consistent and has single quotes/double quotes and square brackets in it.这将是数据本身不一致并且其中包含单引号/双引号和方括号的情况。
You should use objects for this kind of usage like List<Product> stead of List<List>您应该将对象用于这种用途,例如 List<Product> 而不是 List<List>
There is no RecyclerView in Flutter, it's ListView widget. Flutter 中没有 RecyclerView,它是 ListView 小部件。
Your strings should be between quotes ("")您的字符串应该在引号之间(“”)
Here is an example with ListView builder:这是 ListView 构建器的示例:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var list = [["data1", "data2"], ["data1","data2"],["data1","data2"]];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ListView.builder(
itemCount: list.length,
itemBuilder: (context, index) {
return Container(
color: Colors.grey[200],
padding: EdgeInsets.symmetric(vertical: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: List.generate(
list[index].length,
(index2) {
return Text(list[index][index2]);
}
)
),
);
}
)
),
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.