-1

when I use a future builder to get data on the first tab of the bottom navigation bar from the backend it disappears when I change to the second tab bottom navigation bar.

why does it happen?

how do I fix this?

enter image description here

above is the page when I load the app for the first time and API gets hit. enter image description here

above is the page when I change the bottom tab once and go back to the third tab again...

this happens every time...

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'dart:async';




class newExample extends StatefulWidget {
  @override
  newExampleState createState() => newExampleState();
}

class newExampleState extends State<newExample> {

  var userid;


  Future<http.Request> getData() async
  {
    var client = new http.Client();
    final response = await client.get('http://182.0.0.102:5000/user_detail');
    final responseJson = json.decode(response.body);
    dynamic user_id = responseJson['user_id'];


    userid = user_id;
    

    print(responseJson);
    client.close();
    // return responseJson;
  }




  @override
  void initState() {
    super.initState();
    getData();
  }



  @override
  Widget build(BuildContext context) {

    return Scaffold(
      body: Container(
          child: ListView.builder(
            itemCount: userid.length,
            itemBuilder: (BuildContext context, int index){
              return _buildCards(context, index);
            },
          ),
        ),
    );
  }







  Widget _buildCards(BuildContext context, int index){

    return Card(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child:
          // Text('hi'),
          Text('${userid[index]}'),
        )
    );
  }
}


palak
  • 381
  • 7
  • 20
  • 3
    Does this answer your question? [Data is showing only after I hot reload or refresh in flutter](https://stackoverflow.com/questions/59535761/data-is-showing-only-after-i-hot-reload-or-refresh-in-flutter) – Scratte Oct 05 '20 at 12:57

1 Answers1

0

I think it's happening because you call getData() in initState and it will work only once. Try calling your function like this:

 @override
  void initState() {
    SchedulerBinding.instance.addPostFrameCallback((_) {
      getData().then((response) {
        if (mounted)
          setState(() {
            data= response?.something;
          });
      });
    });
    super.initState();
  }

Read about SchedulerBinding: https://api.flutter.dev/flutter/scheduler/SchedulerBinding/addPostFrameCallback.html

s_erfani
  • 476
  • 5
  • 16
  • what is something here? any variable or other? – palak Oct 01 '20 at 07:11
  • @palak i don't know how is your response looks like. for example, I wanted to access fields form my response so: response?.fields – s_erfani Oct 01 '20 at 07:18
  • `Another exception was thrown: NoSuchMethodError: The getter 'visible' was called on null. ════════ Exception caught by rendering library ═════════════════════════════════════════════════════ The method 'debugAssertIsValid' was called on null. Receiver: null Tried calling: debugAssertIsValid()` this is error that I get now – palak Oct 01 '20 at 07:18
  • I don't see visible in your code. where is it? – s_erfani Oct 01 '20 at 07:21
  • yes, I don't see it either..!! and in the above code should I use responseJson where I am getting the data/response? – palak Oct 01 '20 at 07:22
  • didn't you use Visibility widget anywhere? I didn't use responseJson. I can add my code where I'm getting data if you want (I'm using dio package) – s_erfani Oct 01 '20 at 07:27
  • [link] (https://stackoverflow.com/questions/49466556/flutter-run-method-on-widget-build-complete) I did all things in here too.. not working I still need to refresh my app from my side after going back. no, I didn't use a visibility widget anywhere... – palak Oct 01 '20 at 07:33
  • @palak let's try something else. call your getData in setState. – s_erfani Oct 01 '20 at 07:42
  • still not working!! its like the future is not working 2nd time until I hot reload the app...i am getting more confused – palak Oct 01 '20 at 07:56
  • can i use pageView or indexed View? something like that for when data comes it stays – palak Oct 01 '20 at 08:02
  • I'm confused too. it should stay in userId because it's defined out of function. put some prints in functions. – s_erfani Oct 01 '20 at 08:19
  • the value prints on the first time or when I reload the app manually or when I hit the API else the value also gets null. – palak Oct 01 '20 at 08:47