Home > Software design >  How to Convert Two Lists into String Format Flutter
How to Convert Two Lists into String Format Flutter

Time:11-12

Two Lists With User Input

List1 = [0,1,2,3,...];
List2 = [0.1,0.2,0.3,...];

Note: Length of Both Lists is from User Input

I want to Represent Data Like this As a String

var Result = "0 * 0.1   1 *0.2   3 * 0.3 ,......";
print(result);

CodePudding user response:

this result like your example

    final l1 = [0, 1, 2];
    final l2 = [0.1, 0.2, 0.3];
    
    String result = '';
    for (int i = 0; i < l1.length; i  ) {
      final l1Value = l1[i];
      final l2Value = l2[i];
      if (l1Value == null || l2Value == null) {
        break;
      }
      final multiple = '$l1Value * $l2Value';
      result = '$result ${result.isEmpty ? '': ' '} $multiple';
    }

CodePudding user response:

You can use List.join() to achieve your result

  List List1 = [0,1,2,3];

  @override
  Widget build(BuildContext context) {
    return SizedBox(
        height: 400, child: Text(List1.join(",")));
  }

result - 0,1,2,3

    List List1 = [0, 1, 2, 3];
    List List2 = ["a", "b", "c", "D"];
    List mixList = [];
    for (int i = 0; i < List1.length; i  ) {
      mixList.add("${List1[i]}*${List2[i]}");
      print(mixList);
    }
    return Text(mixList.join(","));
  • Related