I’m a new flutter learner. I need to show a list of text in my app in any way.
Please let me know if there more way.
This is my List variable:
List<Text> howtoreach = [
const Text('How to become rich in 7 steps'),
const Text(
'1, Identify your goals. Before you get started on becoming rich, devise a financial plan. ...'),
const Text('2, End your high-interest debt. ...'),
const Text('3, Start budgeting and saving money. ...'),
const Text('4, Pay yourself first. ...'),
const Text('5, Start investing as soon as possible. ...'),
const Text('6, Increase your income. ...'),
const Text('7, Have the right mindset.'),
];
This is my text widget:
Text(
howtoreach,
style:
const TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
textAlign: TextAlign.justify,
),
This is the error I’m getting:
The argument type ‘List’ can’t be assigned to the parameter type ‘String’.
Here I’m including my complete code for reference only
import 'package:flutter/material.dart';
List<Text> howtoreach = [
const Text('How to become rich in 7 steps'),
const Text(
'1, Identify your goals. Before you get started on becoming rich, devise a financial plan. ...'),
const Text('2, End your high-interest debt. ...'),
const Text('3, Start budgeting and saving money. ...'),
const Text('4, Pay yourself first. ...'),
const Text('5, Start investing as soon as possible. ...'),
const Text('6, Increase your income. ...'),
const Text('7, Have the right mindset.'),
];
class DescriptionBlog extends StatelessWidget {
const DescriptionBlog({
super.key,
required this.title,
required this.image,
});
final String title;
final String image;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: PreferredSize(
preferredSize: const Size.fromHeight(50),
child: AppBar(
title: Text(title),
backgroundColor: const Color.fromARGB(255, 43, 9, 75),
toolbarOpacity: 0.99,
),
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
children: [
Image.asset(image),
Text(
title,
style: const TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
color: Colors.green,
),
),
***Text(
howtoreach,***
style:
const TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
textAlign: TextAlign.justify,
),
],
),
),
),
);
}
}
2
Answers
Your
howtoreach
list is list ofwidget
, so you can’t use its item insideText
widget, becauseText
widget only acceptstring
as its data, you have three options.one: change your list to new list that contains a list of
string
like this:two: remove your
Text
widget and return the list’s item like this:three: change your widget to this:
you can do all three way their output is the same. You can replace number
two
andthree
with yourText
widget insideColumn
.In order to populate data from a list of objects in this case List of Text Widgets, you need to use Listview() widget.
ListView constructor creates all items all at once.
You can also try lazy loading items if you have very long list using ListView.builder() widget
ListView.builder() constructor creates items as they’re scrolled onto the screen
Setting shrinkWrap as true will constraint the List to take only the space it requires. If shrinkWrap is not set true, it will take infinite space and throw error. Also you need set scrollPhysics as NeverScrollablePhysics if you have SingleChildScrollView as the Parent Widget.
Output: