Henry
Henry

Reputation: 123

Dynamically grow a listview

I need to dynamically grow a listview.

I have tried the code from How to add the widgets dynamically to column in Flutter? but it appears to be outdated and results in an error (List is deprecated).

Here's my code

var rowWidget = [];

// grow the list
rowWidget.add(
  Row(
    children: [
      Text('111111'),
    ],
),);

...

// display 
ListView(
  padding: EdgeInsets.all(10),
  children: [
    Container(
      child: Column(
       children: [
         rowWidget,
       ],
     ),
   ),
 ],
),

The error which I am getting is Error: A value of type 'List<Row>' can't be assigned to a variable of type 'Widget'.

Upvotes: 0

Views: 584

Answers (2)

Chris Reynolds
Chris Reynolds

Reputation: 5503

Try

Var rowWidget = <Row>[];

Also where you add it to the build try

children : rowWidget,

Or

children : [ …rowWidget ],

Upvotes: 2

puelo
puelo

Reputation: 6037

The error you are getting is due the fact that you are assigning a list in a place where it expects a single Widget. You are basically providing a nested list:

ListView(
  padding: EdgeInsets.all(10),
  children: [
    Container(
      child: Column(
       children: [
         rowWidget,
       ],
     ),
   ),
 ],
),

Should be either:

ListView(
  padding: EdgeInsets.all(10),
  children: [
    Container(
      child: Column(
       children: rowWidget
     ),
   ),
 ],
),

or if you want to spread the list, this also works:

ListView(
  padding: EdgeInsets.all(10),
  children: [
    Container(
      child: Column(
       children: [
         ...rowWidget,
       ],
     ),
   ),
 ],
),

Upvotes: 2

Related Questions