Rajon Moin
Rajon Moin

Reputation: 1

how to get complex json data in future builder dart

{ "count": 6, "next": null, "previous": null, "results": [ { "id": 6, "title": "Java6", "description": "Java Basic", "category": { "id": 1, "name": "Math" }, "sub_category": { "id": 4, "name": "Test" }, "tag": "string", "video": { "id": 6, "duration": 10, "thumbnail": "https://ibb.co/MZkfS7Q", "link": "https://youtu.be/RgMeVbPbn-Q", "views": 0 }, "quiz": { "mcq": [ { "id": 6, "question": "q", "option1": "b", "option2": "c", "option3": "d", "option4": "e", "answer": 1, "appears_at": 0 } ] }, "avg_rating": 1, "total_number_of_rating": 1 },]}

how can I show this JSON data in future builder in dart I have tried this way

Future<dynamic> geteducators() async {
    String serviceurl = "https://api.spiro.study/latest-videos";
    var response = await http.get(serviceurl);
    final jsonrespose = json.decode(response.body);

    print('title: ${jsonrespose}');
    Videos latestVideo = Videos.fromJson(jsonrespose);

    
    return latestVideo.results;
  }

  @override
  void initState() {
    super.initState();
    
    _futureeducators = geteducators();
  }

Upvotes: 0

Views: 782

Answers (2)

RAVI KUMAR
RAVI KUMAR

Reputation: 1

How to use nested future builder for complex json i mean I'm getting name.from same.json in array and other hand I'm getting values from some json but diffrent aaray and array name is similer to uppe

Upvotes: 0

Khurram
Khurram

Reputation: 311

    import 'dart:async';
    import 'dart:convert';
    
    import 'package:flutter/material.dart';
    import 'package:http/http.dart' as http;
    
    Future<Album> fetchAlbum() async {
      final response =
          await http.get(Uri.parse('https://dog.ceo/api/breeds/image/random'));
    
      if (response.statusCode == 200) {
        // If the server did return a 200 OK response,
        // then parse the JSON.
        return Album.fromJson(jsonDecode(response.body));
      } else {
        // If the server did not return a 200 OK response,
        // then throw an exception.
        throw Exception('Failed to load album');
      }
    }
    
    class Album {
      //final int userId;
      final String message;
      final String status;
      // final String region;
      // final String country;
    
      // final String title;
    
      Album({
        //required this.userId,
        required this.message,
        required this.status,
        // required this.country,
        // required this.region,
        //required this.title,
      });
    
      factory Album.fromJson(Map<String, dynamic> json) {
        return Album(
          // userId: json['userId'],
          message: json['message'],
          status: json['status'],
          // country: json['country'],
          // region: json['region'],
          //title: json['total'],
        );
      }
    }
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatefulWidget {
      MyApp({Key? key}) : super(key: key);
    
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      late Future<Album> futureAlbum;
    
      @override
      void initState() {
        super.initState();
        futureAlbum = fetchAlbum();
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Fetch Data Example',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: Scaffold(
            appBar: AppBar(
              title: Text('Fetch Data Example'),
            ),
            body: Center(
              child: FutureBuilder<Album>(
                future: futureAlbum,
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    return Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(snapshot.data!.status),
                        Image(image: NetworkImage(snapshot.data!.message))
                      ],
                    ); //Text(snapshot.data!.ip);
                  } else if (snapshot.hasError) {
                    return Text("${snapshot.error}");
                  }
    
                  // By default, show a loading spinner.
                  return CircularProgressIndicator();
                },
              ),
            ),
          ),
        );
      }

}


//in this way you can get json data and show it in grid or list view.

Upvotes: 1

Related Questions