skip to Main Content
[
    {
      'id': 6,
      'owner': {
        'username': 'ghazali',
        'email': '[email protected]',
        'first_name': 'Abu',
        'last_name': 'Ubaidah',
        'groups': [],
      },
      'name': 'Food Hub',
      'date_created': '2023-12-27T20:13:13.414637Z',
    },
    {
      'id': 7,
      'owner': {
        'username': 'ghazali',
        'email': '[email protected]',
        'first_name': 'Abu',
        'last_name': 'Ubaidah',
        'groups': [],
      },
      'name': 'Hamas',
      'date_created': '2023-12-27T20:04:56.852096Z',
    },
  ];

how can i decode this list?

I want to use this here

children: [
        for (final org in orgs)
          const OrganizationCard(
              id: org['id'],
              tagLine: "We'll find the best for you.",
              organizationName: org['name']),
      ],

i tried multiple kind of typecasting but in vain

3

Answers


  1. import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: Text('Organization Cards'),
            ),
            body: OrganizationList(),
          ),
        );
      }
    }
    
    class OrganizationList extends StatelessWidget {
      final List<Map<String, dynamic>> orgs = [
        {
          'id': 6,
          'owner': {
            'username': 'ghazali',
            'email': '[email protected]',
            'first_name': 'Abu',
            'last_name': 'Ubaidah',
            'groups': [],
          },
          'name': 'Food Hub',
          'date_created': '2023-12-27T20:13:13.414637Z',
        },
        {
          'id': 7,
          'owner': {
            'username': 'ghazali',
            'email': '[email protected]',
            'first_name': 'Abu',
            'last_name': 'Ubaidah',
            'groups': [],
          },
          'name': 'Hamas',
          'date_created': '2023-12-27T20:04:56.852096Z',
        },
      ];
    
      @override
      Widget build(BuildContext context) {
        List<OrganizationCard> organizationCards = orgs.map((org) {
          return OrganizationCard(
            id: org['id'],
            tagLine: "We'll find the best for you.",
            organizationName: org['name'],
          );
        }).toList();
    
        return ListView(
          children: organizationCards,
        );
      }
    }
    
    class OrganizationCard extends StatelessWidget {
      final int id;
      final String tagLine;
      final String organizationName;
    
      const OrganizationCard({
        Key? key,
        required this.id,
        required this.tagLine,
        required this.organizationName,
      }) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Card(
          child: ListTile(
            title: Text('Organization ID: $id'),
            subtitle: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text('Organization Name: $organizationName'),
                Text('Tag Line: $tagLine'),
              ],
            ),
          ),
        );
      }
    }
    
    Login or Signup to reply.
  2. import 'dart:convert';
    
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(
        const MaterialApp(
          home: HomePage(),
        ),
      );
    }
    
    class HomePage extends StatefulWidget {
      const HomePage({super.key});
    
      @override
      State<HomePage> createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage> {
      List<DataModel> data = [
        {
          'id': 6,
          'owner': {
            'username': 'ghazali',
            'email': '[email protected]',
            'first_name': 'Abu',
            'last_name': 'Ubaidah',
            'groups': [],
          },
          'name': 'Food Hub',
          'date_created': '2023-12-27T20:13:13.414637Z',
        }.dataModel,
        {
          'id': 7,
          'owner': {
            'username': 'ghazali',
            'email': '[email protected]',
            'first_name': 'Abu',
            'last_name': 'Ubaidah',
            'groups': [],
          },
          'name': 'Hamas',
          'date_created': '2023-12-27T20:04:56.852096Z',
        }.dataModel,
      ];
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            body: ListView.builder(
          itemCount: data.length,
          itemBuilder: (context, index) {
            return OrganizationCard(
              id: data[index].id,
              tagLine: "We'll find the best for you.",
              name: data[index].name,
            );
          },
        ));
      }
    }
    
    extension CustomExtension on Map<String, dynamic> {
      DataModel get dataModel => DataModel.fromMap(this);
    }
    
    class DataModel {
      final int id;
      final Owner owner;
      final String name;
      final String dataCreated;
    
      DataModel(this.id, this.owner, this.name, this.dataCreated);
    
      Map<String, dynamic> toMap() {
        return <String, dynamic>{
          'id': id,
          'owner': owner.toMap(),
          'name': name,
          'date_created': dataCreated,
        };
      }
    
      factory DataModel.fromMap(Map<String, dynamic> map) {
        return DataModel(
          map['id'] as int,
          Owner.fromMap(map['owner'] as Map<String, dynamic>),
          map['name'] as String,
          map['date_created'] as String,
        );
      }
    
      String toJson() => json.encode(toMap());
    
      factory DataModel.fromJson(String source) =>
          DataModel.fromMap(json.decode(source) as Map<String, dynamic>);
    }
    
    class Owner {
      final String username;
      final String email;
      final String firstName;
      final String lastName;
      final List<dynamic> groups;
    
      Owner(this.username, this.email, this.firstName, this.lastName, this.groups);
    
      Map<String, dynamic> toMap() {
        return <String, dynamic>{
          'username': username,
          'email': email,
          'first_name': firstName,
          'last_name': lastName,
          'groups': groups,
        };
      }
    
      factory Owner.fromMap(Map<String, dynamic> map) {
        return Owner(
            map['username'] as String,
            map['email'] as String,
            map['first_name'] as String,
            map['last_name'] as String,
            List<dynamic>.from(
              (map['groups'] as List<dynamic>),
            ));
      }
    
      String toJson() => json.encode(toMap());
    
      factory Owner.fromJson(String source) =>
          Owner.fromMap(json.decode(source) as Map<String, dynamic>);
    }
    
    class OrganizationCard extends StatelessWidget {
      final int id;
      final String tagLine;
      final String name;
      const OrganizationCard({
        Key? key,
        required this.id,
        required this.tagLine,
        required this.name,
      }) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return ListTile(
          leading: Text(id.toString()),
          title: Text(name),
          subtitle: Text(tagLine),
        );
      }
    }
    
    
    Login or Signup to reply.
  3. You can create a model class out of it. You can use https://app.quicktype.io/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search