I'm new to flutter and i want to convert a List<List> to ArrayList. here's my current list 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]]
I want to add that array list into recyclerView in flutter.
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.
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.
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>
There is no RecyclerView in Flutter, it's ListView widget.
Your strings should be between quotes ("")
Here is an example with ListView builder:
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]);
}
)
),
);
}
)
),
);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.