Search code examples
flutterdart

Dummy data consts fetch, Dart, Flutter


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.


Solution

  • 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.