0

I am trying to display the data from a Consts.dart file in a widget, such as Listview or listview builder. I got errors 'Not String type', etc. How can I make an example here?

I don't want to use classes since I am new. I am drawing UI with dummy data. Also, is this a good way to store data like this for just the UI?

 const dummyData = [
 {
 'imagePath': 'assets/images/blue/1.jpg',
 'category': 'cows',
 'author': 'John',
 'animalImage': 'assets/images/1.jpg',
 'date': '21.15.2004',
 'title': 'Lorem ipsum dollar',
 'content':
 'Sed mi ante, hendrerit at suscipit quis, sodales vitae est. In hac habitasse platea dictumst. Duis laoreet scelerisque mauris. Cras eleifend mauris orci. Ut ante felis, volutpat non sapien id,',
 },
 {
 'imagePath': 'assets/images/white/image_1.jpg',
 'category': 'dogs',
 'author': 'John',
 'animalImage': 'assets/images/1.jpg',
 'date': '21.15.2004',
 'title': 'Sodales vitae est.',
 'content':
 'Sed mi ante, hendrerit at suscipit quis, sodales vitae est. In hac habitasse platea dictumst. Duis laoreet scelerisque mauris. Cras eleifend mauris orci. Ut ante felis, volutpat non sapien id,',
 },
 {
 'imagePath': 'assets/images/green/image_1.jpg',
 'category': 'cats',
 'author': 'John',
 'animalImage': 'assets/images/1.jpg',
 'date': '21.15.2004',
 'title': 'Cats ',
 'content':
 'Sed mi ante, hendrerit at suscipit quis, sodales vitae est. In hac habitasse platea dictumst. Duis laoreet scelerisque mauris. Cras eleifend mauris orci. Ut ante felis, volutpat non sapien id,',
 },
];
 return Scaffold(
 body: ListView.builder(
 itemCount: dummyData.length,
 itemBuilder: (context, index) {
 return Column(
 children: [Image.asset(dummyData), Text(dummyData)],
 );
 },
 ),
 );

A beautiful images of animals and and the content from dummy data to use on the other pages as well.

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
asked Dec 30, 2024 at 9:03

1 Answer 1

2

dummyData is a List<Map<String,String>>, so you can access its items in a list view as the following:

ListView.builder(
 itemCount: dummyData.length,
 itemBuilder: (context, index) {
 return Column(
 children: [
 Image.asset(dummyData[index]['imagePath']),
 Text(dummyData[index]['category'])],
 );
 },
),

where dummyData[index] is a MapEntry (item). You need to access its fields using the key.

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
answered Dec 30, 2024 at 9:48
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you but it says String? can't be assigned to the paramater type 'String' it works after adding '!' is there any solution to solve this in a different way
@melearning, if it's nullable, just decalare dummyData as const List<Map<String,String>> dummyData =....

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.