Taking the following function as example:
List<Widget> getListFiles() {
List<Widget> list = [
Container(),
Container(),
Container(),
];
return list;
}
How to insert into a children param?
Column(
children: <Widget>
[
Text(),
Text(),
getListFiles(), <---
Text(),
]
)
Update
Now Dart has spread operator with the version 2.3.
[
...anotherList
item1
]
Answer without Spread operator
I think you need to spread your list since you can't assign element type 'List' to the list type 'Widget'. Spread operator
is a feature that wanted. You can follow the issue about it on here.
Meanwhile you can use generators
and yield
operator.
Column(
children: List.unmodifiable(() sync* {
yield Text();
yield Text();
yield* getListFiles();
yield Text();
}()),
);
Full code of the usage in widget.
import 'package:flutter/material.dart';
class App extends StatelessWidget {
List<Widget> getListFiles() {
List<Widget> list = [Text('hello'), Text('hello1'), Text('hello2')];
return list;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Stackoverflow'),
),
body: Column(
children: List.unmodifiable(() sync* {
yield Text('a');
yield Text('b');
yield* getListFiles();
yield Text('c');
}()),
),
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With