Home > Net >  Getting this error in flutter:errors.dart:266 Uncaught (in promise) Error: Expected a value of type
Getting this error in flutter:errors.dart:266 Uncaught (in promise) Error: Expected a value of type

Time:11-01

Method for calling API:

 static FutureOr<Iterable<Appointment>> fetchAppointments(String title) async {
    //  final url = Uri.parse('http://192.168.0.134:8080/AtdochubJ-3/appointment/');
    final url = Uri.parse(
        'http://13.233.206.251:8088/AtdochubJ-3/appointment/appointmentdetails');
    final response = await http.get(url);

    if (response.statusCode == 200) {
      final List docs = json.decode(response.body) as List;

      return docs.map((json) => Appointment.fromJson(json)).where((doc) {
        final titleLower = doc.docTitle.toLowerCase();
        final tokenLower = doc.partyName.toLowerCase();
        final searchLower = title.toLowerCase();

        return titleLower.contains(searchLower) ||
            tokenLower.contains(searchLower);
      }).toList();
    } else {
      throw Exception();
    }
  }

The response of API: [{"aptId":297,"aptDate":"2022-10-19","aptPlace":"Kothrud","aptTime":"11:31:00","city":"pune","comments":"No","totalFees":50000,"feesCollected":40000,"partyName":"Swara Joshi","paymentMode":"Cash","staffId":150,"partyType":"Owner","aptStatus":"Open","contactNo":"7845125487","docTitle":"B/204, Spenta Palazzo","tokenNo":22092699900203,"userName":"Satish","docId":267}]

Model Class:

   import 'dart:core';
import 'dart:convert';

Appointment documnetModelJson(String str) =>
    Appointment.fromJson(json.decode(str));

String documentModelToJson(Appointment data) => json.encode(data.toJson());

class Appointment {
  int aptId;
  String aptDate;
  String aptTime;
  String aptPlace;
  String city;
  String? comments;
  int? docId;
  int? feesCollected;
  int totalFees;
  String partyName;
  String? paymentMode;
  int staffId;
  String partyType;
  String docTitle;
  int tokenNo;
  String userName;
  String aptStatus;
  String contactNo;

  int get getaptId => this.aptId;

  set setaptId(aptId) => this.aptId = aptId;

  int? get getdocId => this.docId;

  set setdocId(final docId) => this.docId = docId;

  get getTokenNo => tokenNo;

  set setTokenNo(tokenNo) => this.tokenNo = tokenNo;

  get getDocTitle => docTitle;

  set setDocTitle(docTitle) => this.docTitle = docTitle;

  get getstaffId => this.staffId;

  set setstaffId(staffId) => this.staffId = staffId;

  get getPartyName => this.partyName;

  set setPartyName(partyName) => this.partyName = partyName;

  get getPartyType => this.partyType;

  set setPartyType(partyType) => this.partyType = partyType;

  get getAptDate => this.aptDate;

  set setAptDate(aptDate) => this.aptDate = aptDate;

  get getAptTime => this.aptTime;

  set setAptTime(aptTime) => this.aptTime = aptTime;

  get getAptPlace => this.aptPlace;

  set setAptPlace(aptPlace) => this.aptPlace = aptPlace;

  get getCity => this.city;

  set setCity(city) => this.city = city;

  get getFeesCollected => this.feesCollected;

  set setFeesCollected(feesCollected) => this.feesCollected = feesCollected;

  get getTotalFees => this.totalFees;

  set setTotalFees(totalFees) => this.totalFees = totalFees;

  get getPaymentMode => this.paymentMode;

  set setPaymentMode(paymentMode) => this.paymentMode = paymentMode;

  get getComments => this.comments;

  set setComments(comments) => this.comments = comments;

  get getuserName => this.userName;

  set setuserName(userName) => this.userName = userName;

  get getAptStatus => this.aptStatus;

  set setAptStatus(aptStatus) => this.aptStatus = aptStatus;

  get getContactNo => this.contactNo;

  set setContactNo(contactNo) => this.contactNo = contactNo;

  Appointment(
      {required this.aptId,
      required this.docId,
      required this.tokenNo,
      required this.docTitle,
      required this.staffId,
      required this.partyName,
      required this.partyType,
      required this.aptDate,
      required this.aptTime,
      required this.aptPlace,
      required this.city,
      required this.feesCollected,
      required this.totalFees,
      required this.paymentMode,
      required this.userName,
      required this.aptStatus,
      required this.contactNo,
      required this.comments});

  factory Appointment.fromJson(Map<String, dynamic> json) {
    return Appointment(
        aptId: json['aptId'],
        docId: json['docId'] ?? '',
        tokenNo: json['tokenNo'],
        docTitle: json['docTitle'],
        staffId: json['staffId'],
        partyName: json['partyName'],
        partyType: json['partyType'],
        aptDate: json['aptDate'],
        aptTime: json['aptTime'],
        aptPlace: json['aptPlace'],
        city: json['city'],
        feesCollected: json['feesCollected'] ?? "",
        totalFees: json['totalFees'],
        paymentMode: json['paymentMode'] ?? "",
        userName: json['userName'],
        aptStatus: json['aptStatus'],
        contactNo: json['contactNo'],
        comments: json['comments'] ?? '');
  }

  Map<String, dynamic> toJson() => {
        'aptId': aptId,
        'docId': docId,
        'tokenNo': tokenNo,
        'docTitle': docTitle,
        'staffId': staffId,
        'partyName': partyName,
        'type': partyType,
        'aptDate': aptDate,
        'aptTime': aptTime.toString(),
        'aptPlace': aptPlace,
        'city': city,
        'feesCollected': feesCollected,
        'totalFees': totalFees,
        'paymentMode': paymentMode,
        'userName': userName,
        'aptStatus': aptStatus,
        'contactNo': contactNo,
        'comments': comments,
      };
}

The UI:

import 'package:AtDocHUB/View/AppointmentPageFE.dart';
import './AppointmentsDetailsEditPage.dart';
import 'dart:core';

import 'package:AtDocHUB/Controller/AppointmentController.dart';
import 'package:AtDocHUB/Model/Appointment.dart';
import 'package:get/instance_manager.dart';
import 'package:flutter/material.dart';

class AppointmentDetails extends StatefulWidget {
  final int aptId;
  //final int docId;
  //final int staff_Id;

  AppointmentDetails(
    this.aptId,
    // this.docId,
    // this.staff_Id,
  );

  @override
  _AppointmentDetailsState createState() => _AppointmentDetailsState(
        this.aptId,
        // this.docId,
        //this.staff_Id,
      );
}

class _AppointmentDetailsState extends State<AppointmentDetails> {
  AppointmentController appointmentController =
      Get.put(AppointmentController());

  late Future<Appointment> futureAppointments;
  final int aptId;
  //final int docId;
  // final int staff_Id;
  _AppointmentDetailsState(
    this.aptId,
    //  this.docId,
    // this.staff_Id,
  );

  @override
  void initState() {
    super.initState();
    // futureDocuments = documentController
    //     .fetchDocumentsByTitle('Skyone Building, Flat no 101');
    // futureDocuments = documentController.fetchDocumentsByID(docId);
    futureAppointments = AppointmentController.fetchAppointmentsById(this.aptId)
        as Future<Appointment>;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color.fromARGB(255, 3, 87, 156),
        title: Text('Appointment Details'),
        leading: IconButton(
            icon: BackButtonIcon(),
            onPressed: () => Navigator.of(context).push(MaterialPageRoute(
                builder: (BuildContext context) => AppointmentPageFE()))),
      ),
      body: Center(
        child: FutureBuilder<Appointment>(
          future: futureAppointments,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return ListView(children: [
                SafeArea(
                  child: SingleChildScrollView(
                    child: Container(
                      padding: EdgeInsets.all(15),
                      height: 750,
                      // child: Card(
                      //
                      child: SelectionArea(
                        child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceAround,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                  "Document Title : "  
                                      snapshot.data!.partyName,
                                  style: const TextStyle(fontSize: 15.0)),
                              Text("Token No : "   snapshot.data!.partyName,
                                  style: const TextStyle(fontSize: 15.0)),
                              Text("Party Name : "   snapshot.data!.partyName,
                                  style: const TextStyle(fontSize: 15.0)),
                              // Text(
                              //     "Appointment Executive : "  
                              //         snapshot.data!.userName,
                              //     style: const TextStyle(fontSize: 15.0)),
                              Text("Contact No : "   snapshot.data!.contactNo,
                                  style: const TextStyle(fontSize: 15.0)),
                              Text("Party Type : "   snapshot.data!.partyType,
                                  style: const TextStyle(fontSize: 15.0)),
                              Text(
                                  "Appointment Place : "  
                                      snapshot.data!.aptPlace,
                                  style: const TextStyle(fontSize: 15.0)),
                              Text("Appointment City : "   snapshot.data!.city,
                                  style: const TextStyle(fontSize: 15.0)),
                              Text(
                                  "Appointment Date : "  
                                      snapshot.data!.aptDate,
                                  style: const TextStyle(fontSize: 15.0)),
                              Text(
                                  "Appointment Time : "  
                                      snapshot.data!.aptTime,
                                  style: const TextStyle(fontSize: 15.0)),
                              Text(
                                  "Appointment Status : "  
                                      snapshot.data!.aptStatus,
                                  style: const TextStyle(fontSize: 15.0)),
                              Text("Fees : "   snapshot.data!.partyType,
                                  style: const TextStyle(fontSize: 15.0)),
                              Text(
                                  "Fees Collected : "  
                                      snapshot.data!.partyType,
                                  style: const TextStyle(fontSize: 15.0)),
                              Text("Payment Mode : "   snapshot.data!.partyName,
                                  style: const TextStyle(fontSize: 15.0)),

                              // Text("Comments : "   snapshot.data!.comments!,
                              //     style: const TextStyle(fontSize: 15.0)),
                              //  ])),
                              // ),

                              // child: Row(
                              //   children: [
                              // SizedBox(
                              //   width: 100,
                              // ),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Container(
                                    alignment: Alignment.center,
                                    height: 35,
                                    width: 200,
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(10),
                                      color: Color.fromARGB(255, 3, 89, 168),
                                    ),
                                    child: TextButton(
                                      // style:
                                      //     TextButton.styleFrom(fixedSize: Size.fromHeight(300)),
                                      child: const Text(
                                        'Edit ',
                                        style: TextStyle(
                                          fontSize: 17.0,
                                          fontWeight: FontWeight.bold,
                                          color: Colors.white,
                                          backgroundColor:
                                              Color.fromARGB(255, 3, 89, 168),
                                        ),
                                      ),

                                      onPressed: () {
                                        Navigator.of(context)
                                            .pushAndRemoveUntil(
                                                MaterialPageRoute(
                                                    builder: (BuildContext
                                                            context) =>
                                                        AppointmentDetailsEditPage(
                                                          this.aptId,
                                                          //  this.docId,
                                                          // this.staff_Id,
                                                        )),
                                                (Route<dynamic> route) =>
                                                    false);
                                      },
                                    ),
                                  ),
                                ],
                              ),
                            ]),
                      ),
                    ),

                    // ),
                    padding: const EdgeInsets.all(10),
                  ),
                ),
              ]);
            } else if (snapshot.hasError) {
              return Text('${snapshot.error}');
            }

            // By default, show a loading spinner.
            return const CircularProgressIndicator();
          },
        ),
      ),
    );
  }
}

CodePudding user response:

I suspect null is coming from different item, provide default value on null case.

  factory Appointment.fromJson(Map<String, dynamic> json) {
    return Appointment(
        aptId: json['aptId'] ?? 0, 
        /// same for other int
        ...);
  }

CodePudding user response:

One of your int fields got null value, you must check aptId, staffId, tokenNo

  • Related