skip to Main Content

In my code I want fetch data to backend without show in ui. Data getting from API, andaslo for that I use model class that same model and API call I used to fetch data and show in UI. That’s work without any errors.But in this page I want get doctor_in vale is true or false from that same model and API call method.

model class

   class DataDoctor {
  String appId;
  String channelName;
  String receiver_name;
  bool doctor_in;

  DataDoctor(
      {required this.appId,
      required this.channelName,
      required this.receiver_name,
      required this.doctor_in});

  factory DataDoctor.fromJson(Map<String, dynamic> json) {
    return DataDoctor(
      appId: json['appId'] == null ? null : json['appId'],
      channelName: json['channelName'] == null ? null : json['channelName'],
      receiver_name:
          json['receiver_name'] == null ? null : json['receiver_name'],
      doctor_in: json['doctor_in'] == null ? null : json['doctor_in'],
    );
  }
}

using this model I want get doctor_in boolean value
to the getDoctorActive() method

getDoctorActive() method

void getDoctorActive() {
    Map<String, dynamic> jsonData =
        json.decode(jsonDataAsString) as Map<String, dynamic>;
    doctor_in.value = jsonData['doctor_in'].toString();   }

error

enter image description here

How to get data without show in UI in flutter?

API code

import 'dart:convert';
import 'package:http/http.dart';
import '../model/appIdModel.dart';


class ApiService {
  loadData(String channelName) async {
    final String url ='https://jsonplaceholder.typicode.com/posts/1=$channelName';

    Future<List<Data>> getData() async {
      Response response = await get(Uri.parse(url));
      if (response.statusCode == 2000) {
        Map<String, dynamic> json = jsonDecode(response.body);
        List<dynamic> body = json['data'];

        List<Data> datas = body.map((dynamic item) => Data.fromJson(item).toList();
            return datas;
            } else {
            throw ('cannot fetch data');
            }
        }
  }


}

initState

Timer? timer;
  bool doctor_in = false;
  @override
  void initState() {
    super.initState();
    getDoctorActive();
    timer =
        Timer.periodic(Duration(seconds: 15), (Timer t) => checkDoctorActive());
  }

checkDoctorActive

 Future<void> checkDoctorActive() async {
    if (doctor_in == true) {
      future = client.getData(widget.channelName);
    }
  }

errors

enter image description here

API call

enter image description here

2

Answers


  1. If you want to periodically fetch data in the background without updating the UI, you can create a class for that purpose, like this

    class DoctorCheck{
      Future<bool> isDoctorActive(String channelName) async {
        // do the api call here as shown in the line below
        // var jsonResponse = await client.getData(widget.channelName)
        return Data.fromJson(jsonResponse).doctor_in == true;
      }
    }
    

    And call it wherever you want, like this

    bool isDoctorActive = await DoctorCheck().isDoctorActive(channelName);

    It will return a bool whether the doctor is active or not.

    Put it in a function like this

    Future<void> dr() async { 
      bool isDrActive = await DoctorCheck().isDoctorActive(channelName); 
      setState(() { doctor_in = isDrActive; }); 
    }
    

    Whenever you call dr(), your variable doctor_in will be updated with the latest value of whether doctor is active or not.

    Login or Signup to reply.
  2. From @rrttrr answer with a change

    class DoctorCheck{
      Future<bool> isDoctorActive(String channelName) async {
        return Data.fromJson(json).doctor_in == true; // Change jsonResponse to json
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search