flutterdart

Dummy data consts fetch dart, flutter


Can someone help me please. I am trying to display the data from Consts.dart file in a widget such as Listview or listview builder. I got errors 'Not String type' etc. I am new to this. I appreciate if anyone help me to make an example here. I don't want to use classes since I am new. I am drawing UI with a dummy data. Also is this a good way to storage data like this for just UI. Sorry i am learning, checked examples and they were too complicated for me.

 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 it's 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 it's fields using the key.