1

I have this two arrays and want to generate Column Widget like below.

class ProfileWidget extends StatelessWidget {
 @override
  Widget build(BuildContext context) {
    List<FaIcon> infoIcon = [
      FaIcon(FontAwesomeIcons.idCard),
      FaIcon(FontAwesomeIcons.at),
    ];

    List infoText = [
      'AAA',
      '[email protected]',
    ];

    return Column(
      children: [
        // divider box
        Center(
          child: Container(
            child: Row(
              children: <Widget>[
                Expanded(
                  child: Divider(
                    color: Colors.orange,
                    height: 40,
                    thickness: 2,
                    indent: 30,
                    endIndent: 10,
                  ),
                ),
                Text(
                  "Info",
                  style: TextStyle(
                      color: Colors.lightGreen, fontWeight: FontWeight.bold),
                ),
                Expanded(
                  child: Divider(
                    color: Colors.blue,
                    height: 40,
                    indent: 10,
                    endIndent: 30,
                    thickness: 2,
                  ),
                ),
              ],
            ),
          ),
        ),
        List<Widget>.generate(infoIcon.length, (index) {
          return Padding(
            padding: const EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 5.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Container(
                  width: 120,
                  child: Center(
                    child: FaIcon(
                      infoIcon[index],
                    ),
                  ),
                ),
                Container(
                  width: 175,
                  child: Text(
                    infoText[index],
                    overflow: TextOverflow.ellipsis,
                    style: notNotDefaultStyle,
                  ),
                )
              ],
            ),
          );
        }),
      ],
    );
  }
}

I get an error like

The element type 'List<Widget>' can't be assigned to the list type 'Widget'

So, I changed to dynamic and then I also receive Generates a list of values. Creates a list with [length] positions and fills it with values created by calling [generator] for each index in the range 0 .. length - 1 in increasing order.

I even delete <> but then, still it does not work. How can I generate widgets with two arrays using List.generate() in flutter?

1 Answer 1

4
Column(
   children: List<Widget>.generate(infoIcon.length, (index) {
      return Container(
         child: Text('${infoText[index]}') 
      );
    },
  )
)
Sign up to request clarification or add additional context in comments.

6 Comments

` (new) List<Widget> List.generate(int length, Widget Function(int) generator, {bool growable = true}) dart:core Generates a list of values. Creates a list with [length] positions and fills it with values created by calling [generator] for each index in the range 0 .. length - 1 in increasing order. List<int>.generate(3, (int index) => index * index); // [0, 1, 4] The created list is fixed-length if [growable] is set to false. The [length] must be non-negative. The element type 'List<Widget>' can't be assigned to the list type 'Widget'` I still get this error
Use "List<Widget>.generate" inside a Column.
It is inside the column?
Did you review the sample code I shared? Create a "column" after your divider box and put "list generator" inside it's children field. "list generator" not return a widget, it return a widget list. You can not put "widget list" inside a field waiting for a widget.
I just did but now I am getting The element type 'List<dynamic>' can't be assigned to the list type 'Widget' this error? tried using List<Widget> and other stuffs as wel.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.