1

The below given is my response model and I am able to display the value of message but now I want to display the value of name which is inside result I have attached the image how I am displaying the data of message now how do i display it?

{
    "message": "sucess",
    "error": false,
    "code": 200,
    "result": [
        {
            "id": 1,
            "name": "Lab Report"
        },
        {
            "id": 2,
            "name": "News"
        },
        {
            "id": 3,
            "name": "X-ray"
        },
        {
            "id": 8,
            "name": "Blood Test"
        }
    ],
    "status": 200
}

This is how I display the data of message

This is how I am displaying the value of message using future builder

Column(
              children: [
                 FutureBuilder<Categories>(
                     future: futureAlbum,
                     builder: (context,snapshot){
                   if(snapshot.hasData){
                     return Text(snapshot.data!.message ?? "");
                   }else if(snapshot.hasError){
                     return Text("${snapshot.error}");
                   }   return const CircularProgressIndicator();
                 })
              ],
            ),

This is my model class

class Categories {
  String? message;
  bool? error;
  int? code;
  List<Result>? result;
  int? status;

  Categories({this.message, this.error, this.code, this.result, this.status});

  Categories.fromJson(Map<String, dynamic> json) {
    message = json['message'];
    error = json['error'];
    code = json['code'];
    if (json['result'] != null) {
      result = <Result>[];
      json['result'].forEach((v) {
        result!.add(new Result.fromJson(v));
      });
    }
    status = json['status'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['message'] = this.message;
    data['error'] = this.error;
    data['code'] = this.code;
    if (this.result != null) {
      data['result'] = this.result!.map((v) => v.toJson()).toList();
    }
    data['status'] = this.status;
    return data;
  }
}

class Result {
  int? id;
  String? name;

  Result({this.id, this.name});

  Result.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    return data;
  }
}

This is Api class

import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'ModelsForCategories.dart';

class RemoteService {
  Future<Categories> fetchAlbum() async {
    final response = await http.get(Uri.parse("http://10.0.2.2:8000/api_vi/getcategories/"));

    if (response.statusCode == 200) {
      return Categories.fromJson(json.decode(response.body));
    } else {
      throw Exception('Failed to load album');
    }
  }
}
4
  • Share your Categories model code. Commented Nov 2, 2022 at 10:57
  • can you share your api and console log? Commented Nov 2, 2022 at 11:43
  • @SuganPandurengan, it did fetch the data when I used how u said, but I need to fetch every data for name, like when u did [0], it only fetched 0th index data but I want every data which is under "name" then how do I do it? Commented Nov 2, 2022 at 14:11
  • @DipakRamoliya, done. and console is the same as the response which is posted at top. Commented Nov 2, 2022 at 14:12

1 Answer 1

1

Try snapshot.data!.result[0].name

For instance if you want to display the name in the first of the result list.

Column(
              children: [
                 FutureBuilder<Categories>(
                     future: futureAlbum,
                     builder: (context,snapshot){
                   if(snapshot.hasData){
                     return Text(snapshot.data?.result[0]?.name ?? "");
                   }else if(snapshot.hasError){
                     return Text("${snapshot.error}");
                   }   return const CircularProgressIndicator();
                 })
              ],
            ),
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.