Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.2 kB
5
Indexable
Never
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // Two example lists
  List<String> list1 = ['Item 1', 'Item 2', 'Item 3'];
  List<String> list2 = ['Item A', 'Item B', 'Item C'];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Combined List Example'),
        ),
        body: ListView.builder(
          itemCount: _calculateMinLength(list1.length, list2.length),
          itemBuilder: (context, index) {
            // Access elements from both lists at the same index
            String itemFromList1 = index < list1.length ? list1[index] : '';
            String itemFromList2 = index < list2.length ? list2[index] : '';

            // You can use both items in your ListTile or any other widget
            return ListTile(
              title: Text('$itemFromList1 - $itemFromList2'),
            );
          },
        ),
      ),
    );
  }

  // Calculate the minimum length to avoid index out of bounds
  int _calculateMinLength(int length1, int length2) {
    return length1 < length2 ? length1 : length2;
  }
}
Leave a Comment