2

In my code when open this page then call fetchData() method in initState.fetchData methos call every 15 seconds. initState

Timer? timer;
  Future<List<DataDoctor>>? future;
  @override
  void initState() {
    super.initState();

    timer = Timer.periodic(Duration(seconds: 15), (Timer t) => fetchData());
  }

in fetchData method geting data from API call in the API has these data.

[![enter image description here][2]][2] those data should retrieve to backend for now I only retrieve only Boolean value. like as my code this Boolean value I get as doctor_in variable. if doctor_in false then should recall again API and should get data. it should at every 15 seconds until open this page.

data retrieve code

  FutureBuilder<DataDoctor> fetchData() {
    builder:
    (context, snapshot) {
      if (snapshot.hasData) {
        if (snapshot.data!.doctor_in == false) {
          future = client.isDoctorActive();
        }
      } else if (snapshot.hasError) {
        return Text('${snapshot.error}');
      }
    };
  }

model

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

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

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

api call

class ApiServiceDataDoctor {
  final String url =
      'https://tools.learningcontainer.com/sample-json-file.json';
  Future<List<DataDoctor>> isDoctorActive() async {
    Response response = await get(Uri.parse(url));
    if (response.statusCode == 200) {
      Map<String, dynamic> json = jsonDecode(response.body);

      Map<String, dynamic> body = json['dataActive'];
      List<DataDoctor> datas = [DataDoctor.fromJson(body)];
      return datas;
    } else {
      throw ('cannot fetch data');
    }
  }
}

if doctor_in == true then ElevatedButton should enable

Dasun Dola
  • 541
  • 2
  • 16
  • you can use `èlse` as default return from the `fetchData` – Md. Yeasin Sheikh Dec 31 '22 at 09:05
  • I didn't get it – Dasun Dola Dec 31 '22 at 09:10
  • try `FutureBuilder fetchData() { builder: (context, snapshot) { if (snapshot.hasData) { if (snapshot.data!.doctor_in == false) { future = client.isDoctorActive(); } } else if (snapshot.hasError) { return Text('${snapshot.error}'); } else { return Text("NA state"); } }; } ` – Md. Yeasin Sheikh Dec 31 '22 at 09:24
  • show this error , The body might complete normally, causing 'null' to be returned, but the return type, 'FutureBuilder', is a potentially non-nullable type – Dasun Dola Dec 31 '22 at 09:32
  • I am not getting the `fetchData` method, is it properly included ? maybe you are missing `return builder`, also return something inside `if` state – Md. Yeasin Sheikh Dec 31 '22 at 09:34
  • okay ... do know how to solve that error – Dasun Dola Dec 31 '22 at 09:35
  • 1
    I think your `fetchData` isnot properly included on question, you are following FutureBuilder, but retruning it from the method – Md. Yeasin Sheikh Dec 31 '22 at 09:39
  • 1
    It could be `FutureBuilder fetchData() { return FutureBuilder(builder: (context, snapshot) { if (snapshot.hasData) { if (snapshot.data!.doctor_in == false) { future = client.isDoctorActive(); } return DataDoctor(); // a instance } else if (snapshot.hasError) { return Text('${snapshot.error}'); } else { return Text("NA state"); } }); }` – Md. Yeasin Sheikh Dec 31 '22 at 09:42
  • error solved bro thank you. and also do you know "snapshot.data!.doctor_in" this boolean value pass to page backend. In my code i want this boolean value to enable or disable button – Dasun Dola Dec 31 '22 at 10:13
  • The `snapshot` isn't available, I think the widget tree is needed to be changed. – Md. Yeasin Sheikh Dec 31 '22 at 10:17
  • yeah okay . I got it . I will try , I think I have pass "snapshot.data!.doctor_in" to backend from FutureBuilder fetchData() and declare to another boolean variable . Then I hope I can use in widgets. Am I correct? – Dasun Dola Dec 31 '22 at 12:35
  • What I will do, use this as a child on FutureBuilder, also My previous comment as an issue `return DataDoctor();` . – Md. Yeasin Sheikh Dec 31 '22 at 13:14

2 Answers2

1

Simply make the future nullable or provide some default value. It is throwing exception because the value can be null which will crash the application.

Hamza Malik
  • 86
  • 1
  • 7
1
FutureBuilder<DataDoctor> fetchData() {
    builder:
    (context, snapshot) {
      if (snapshot.hasData) {     // ⚠ This if statement is not returning anything, So you are getting error
        if (snapshot.data!.doctor_in == false) {
          future = client.isDoctorActive();
          return Text(`${snapshot.data}`);
        }
       if (snapshot.hasError) {
        return Text('${snapshot.error}');
      }
       return Text(`${snapshot.error}`) //  Add this return statement 
    };
  }
krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88
  • Thank you for you support.When I try your code at the "FutureBuilder fetchData()" show this error "The body might complete normally, causing 'null' to be returned, but the return type, 'FutureBuilder', is a potentially non-nullable type. " – Dasun Dola Dec 31 '22 at 12:39
  • Can you please clean up the code and just present the error image of the updated code and the code in the image seperately in the question and delete rest of the code. I might be able to help – krishnaacharyaa Dec 31 '22 at 12:47