How to have multiple arguments to a list in Flutter - flutter

I have a function which returns a List of ListTiles.
List<ListTile> tinyImage = [];
List<ListTile> croppedImages() {
ListTile newImage =
ListTile(title: crop1 == null ? Text('No Image') : ListPart());
tinyImage.add(newImage);
return tinyImage;
}
But i want this list to have other arguments like Text, DrawerHeader and other stuffs. Like,
List<ListTile, DrawerHeader> tinyImage = [];
I am trying to create a Drawer. For now i am able to return a list of images with the above function. I want to add a header which is not possible at the moment. Please help me out.

You have to set the list type to something more generic, like Widget.
List<Widget> items = [];
items.add(DrawerHeader());
items.add(ListTile());

Related

Dynamically added widget state is null in Flutter

I am developing a feature where users can press a button to add a new group of text fields to the screen. Each group of text fields is stored in its own stateful widget. The abridged code to add the new widget is shown below:
List<EducationField> fieldList = [];
List<GlobalKey<EducationFieldState>> keyList = [];
// Function that adds new widgets to the list view
onTap: () {
GlobalKey<EducationFieldState> key = new GlobalKey<EducationFieldState>();
setState(() {
fieldList.add(EducationField(key: key));
keyList.add(key);
});
},
I can dynamically add the new widgets just fine. However when I try to access the state of the widgets, I get an error saying that the state of the respective widget is null. There is a function in each widget state that gets the values from their text fields. The code I'm using to do that is also shown below:
void _getUserData(){
List<EducationModel> modelList = [];
for(int i = 0; i < fieldList.length; i++){
modelList.add(keyList[i].currentState!.getData()); // this line is causing the error
modelList.last.printModel();
}
}
I have done a lot of research on this issue and still have no idea why I am getting a null error. Is my approach wrong or is it something more minor? I can also give more code if necessary.
Thanks in advance!
Checkout GlobalKey docs.
Your dynamically added Text widget doesn't exist in the Widget tree yet, as you just created it and you're trying to add it to the Widget tree. So it doesn't have a current state.
Maybe this helps?
keyList[i].currentState?.getData() ?? '' // I'm guessing getData return a String
Something else you could try:
// only call _getUserData() after widget finished building
WidgetsBinding.instance!.addPostFramecallback(() => _getUserData());

Why Expanded widget can not add a child which is Text in Dart?

When I add a Expanded widget whose child is Text into a list,Android Studio throw a error message like this:
Error: The argument type 'Expanded' can't be assigned to the parameter type 'Text'.
final List<Text> words = [];
words.add(Expanded(child: Text('hello')));
enter image description here
Your list is a List of Text widget and you are trying to add an Expanded widget to it.
try this:
final List<Expanded> words = [];
words.add(Expanded(child: Text('hello')));
or:
final List<Text> words = [];
words.add(Text('hello'));
or:
final List<Widget> words = [];
words.add(Expanded(child: Text('hello')));
Expanded is a widget that can be used with(inside) Row, Column and Flex. You can't add it to a list of type Text, just remove it.
Try this:
final List<Text> words = [];
words.add(Text('hello'));

I have a stored List data and how can I localize it in flutter?

I can translate the text inside the widget using AppLocalizations.of(context)!.translated and it work fine
However, when I want to localize a stored List<> data which will be used in listView Builder, I am not able to do so, since it need (context).
My question is how to localize a stored list data(outside widget) and pass to list view builder?
Thanks for your help.
I think you should do 2 things
In case if you have a list in another file, then it should be static like this:
class Presets {
static var presetData = [];
}
after you loaded up your list, you can do these steps:
First,
Create Widgets as children like this:
List<Widget> presetsLoadUp() {
List<Widget> children = <Widget>[];
for(i < yourList.length){
children.add(Text(yourList[i]));
}
return children;
}
You can create your own unique widget inside the children.add method.
Secondly,
Create the list:
...
child: ListView(
children: presetsLoadUp(),
),
If anyone has similar problem, please take it for reference.
I just build another widget to solve this.
What I do is :
build another widget,
then put the list data inside and
return ListViewBuilder.
Everything is same,
just put your list data inside another widget can solve the problem

How to add items that generated through random method to a list and display it?

By using random function it displayed some of random element from list in a container how can i add that specific item into a list and display it as favourites? Please help me with this.
List<String> favourites = [];
onLongPress: () {
setState(() {
randomIndex = Random().nextInt(hes.length);
favourites.add(hes[randomIndex].name);
});
},
you just need to add a random element into a list and display it using listview builder may be

How to create function which return array of widgets

I have to create a function which returns array of widgets, like this:
new GridView.count(crossAxisCount: _column,children: getRandomWidgetArray(),
Example function like this:-
Widget[] getRandomWidgetArray()
{
Widget[] gameCells;
for(i=0;i<5;i++)
{
gameCells.add(new MyCellWidget(i));
}
return gameCells;
}
Above code is giving this error:
I know how to do it without function:
children: < Widget >[ new MyCellWidget(0),
new MyCellWidget(1),]
But I have to make it dynamic with function as values will change in future, above code is just prototype. Flutter examples are very few.
Array types are List<Type> in Dart:
List<Widget> getRandomWidgetArray()
[] can only be used as literal value to create a new list value, not for type declaration.
At this time, the following code is deprecated:
List <Widget> gameCells = List<Widget>();
Use the following instead:
List <Widget> gameCells = <Widget>[];
The first code will work anyway, but you will have a warning.
List<Widget> getRandomWidgetArray(){
List <Widget> gameCells = List<Widget>();
for(i=0;i<5;i++)
{
gameCells.add(new MyCellWidget(i));
}
return gameCells;
}
This is the correct way of initializing List, adding elements and returning Widget list.