'unexpected null value while using futureBuilder in flutter

I am am trying fetch data from api, using futureBuilder to show data in application but getting null in return,

I have checked everywhere i could not find any solution for this error


here is my model class

import 'dart:convert';

List<RestrauntMenu> postFromJson(String str) => List<RestrauntMenu>.from(
json.decode(str).map((x) => RestrauntMenu.fromMap(x)));

class RestrauntMenu {
int? rMid;
String? itemname;
String? itemPrice;
String? itemDescription;
String? itemimage;
String? itemTag;
String? catname;
int? rid;

RestrauntMenu(
  {this.rMid,
  this.itemname,
  this.itemPrice,
  this.itemDescription,
  this.itemimage,
  this.itemTag,
  this.catname,
  this.rid});

 factory RestrauntMenu.fromMap(Map<String, dynamic> json) => RestrauntMenu(
    rid: json['rid'],
    rMid: json['rMid'],
    itemname: json['itemname'],
    itemPrice: json['itemPrice'],
    itemDescription: json['itemDescription'],
    itemimage: json['itemimage'],
    itemTag: json['itemTag'],
    catname: json['catname'],
  );

}

here i am using and fetching data

import 'package:flutter/material.dart';
import 'package:fooddeliveryapp/apis.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'models/restrauntmenu.dart';

class RestrauntPage extends StatefulWidget {
  final String restrauntId;
  final String restrauntName;
  final String restrauntType;
  final String restrauntApproxBill;
  final String restrauntTagline;

  const RestrauntPage(
      {Key? key,
      required this.restrauntId,
      required this.restrauntType,
      required this.restrauntApproxBill,
      required this.restrauntTagline,
      required this.restrauntName})
      : super(key: key);
  @override
  State<RestrauntPage> createState() => _RestrauntPageState();
}

class _RestrauntPageState extends State<RestrauntPage> {
  late Future<List<RestrauntMenu>> futureRestrauntMenu;
  Future<List<RestrauntMenu>> fetchRestrauntMenu() async {
    String restrauntId = widget.restrauntId;
    final response = await http.get(Uri.parse(menuApi(restrauntId)));

    if (response.statusCode == 200) {
      final parsed = json.decode(response.body).cast<Map<String, dynamic>>();

      return parsed
          .map<RestrauntMenu>((json) => RestrauntMenu.fromMap(json))
          .toList();
    } else {
      throw Exception('Failed to load album');
    }
  }

  @override
  void initState() {
    futureRestrauntMenu = fetchRestrauntMenu();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        foregroundColor: Colors.black,
        elevation: 0,
        backgroundColor: Colors.white,
        title: const Text('Restraunt Name'),
      ),
      body: SingleChildScrollView(
        child: SizedBox(
          height: MediaQuery.of(context).size.height,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  SizedBox(
                    width: MediaQuery.of(context).size.width * 0.03,
                  ),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        widget.restrauntName,
                        style: const TextStyle(
                          color: Colors.black,
                          fontSize: 24,
                          fontWeight: FontWeight.w900,
                        ),
                      ),
                      // const SizedBox(height: 4),
                      Text(
                        widget.restrauntType,
                        style: const TextStyle(
                          color: Colors.black,
                          fontSize: 16,
                          fontWeight: FontWeight.normal,
                        ),
                      ),
                      Text(
                        widget.restrauntTagline,
                        style: const TextStyle(
                          color: Colors.black87,
                          fontSize: 12,
                          fontWeight: FontWeight.normal,
                        ),
                      ),
                      const SizedBox(
                        height: 6,
                      ),
                      Container(
                        decoration: BoxDecoration(
                          color: Colors.grey[200],
                          borderRadius: BorderRadius.circular(10),
                        ),
                        child: Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: RichText(
                            text: TextSpan(
                              children: [
                                const WidgetSpan(
                                  child: Icon(
                                    Icons.currency_rupee,
                                    size: 16,
                                    color: Color.fromARGB(255, 45, 174, 49),
                                  ),
                                ),
                                TextSpan(
                                  text: widget.restrauntApproxBill,
                                  style: const TextStyle(
                                    color: Colors.black,
                                    fontSize: 12,
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                  const Spacer(),
                  Container(
                    decoration: BoxDecoration(
                      color: const Color.fromARGB(255, 49, 171, 53),
                      borderRadius: BorderRadius.circular(12),
                    ),
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: RichText(
                        text: const TextSpan(
                          children: [
                            TextSpan(
                              text: '3.6',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 16,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                            WidgetSpan(
                              child: Icon(
                                Icons.star,
                                color: Colors.white,
                                size: 18,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                  SizedBox(
                    width: MediaQuery.of(context).size.width * 0.03,
                  ),
                ],
              ),
              const SizedBox(
                height: 12,
              ),
              Expanded(
                child: FutureBuilder<List<RestrauntMenu>>(
                    future: futureRestrauntMenu,
                    builder: (context, snapshot) {
                      if (snapshot.hasData) {
                        return ListView.builder(
                            itemCount: 1,
                            itemBuilder: (context, index) {
                              return ExpansionTile(
                                title: Text(snapshot.data![index].itemname!,
                                    style: const TextStyle(
                                        fontSize: 20,
                                        fontWeight: FontWeight.bold)),
                              );
                            });
                      } else if (snapshot.hasError) {
                        return Text("${snapshot.error}");
                      }
                      return const Center(
                        child: CircularProgressIndicator(),
                      );
                    }),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

please tell me if i am missing something or making some mistake while fetching or using data,

please check my model class too and let me know if i am making mistake in model class....



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source