Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display Image if my list is empty?

I want to display an Image or Text in Center like No Record Found all details are fetched from API. In Native Android setEmptyView() for this condition but no idea how can I do this in flutter. Non-stop progress Dialog is running but the text is not displayed. I added code with my JSON response

List<NewAddressModel> newAddress = List();
bool isLoading = false;

Scaffold(
body :isLoading
          ? Center(
        child: CircularProgressIndicator(),
      )
          : Container(
          color: Color.fromRGBO(234, 236, 238, 1),
          child: getListView()
      ),
)
Widget getListView(){
    return newAddress.isNotEmpty ?
    ListView.builder(itemCount: newAddress.length,
        itemBuilder: (BuildContext context, int index) {
          return addressViewCard(index);
        }

    )
        : Center(child: Text("No Found"));
  }
Future<List>hitGetAddressApi() async {
    setState(() {
      isLoading = true;
    });
    final response = await http.post(api);

    var userDetails = json.decode(response.body);
    if (response.statusCode == 200) {
      newAddress = (json.decode(response.body) as List)
          .map((data) => new NewAddressModel.fromJson(data))
          .toList();
      setState(() {
        isLoading = false;
        newAddressModel = new NewAddressModel.fromJson(userDetails[0]);
      });
    }


    return userDetails;
  }
This is JSON Response which I am getting from my API.
[
    {
        "id": "35",
        "name": "Toyed",
        "mobile": "8855226611",
        "address": "House No 100",
        "state": "Delhi",
        "city": "New Delhi",
        "pin": "000000",
        "userid": "2",
    }
]

like image 572
Jorden Avatar asked Mar 03 '23 21:03

Jorden


1 Answers

I would like to tell you that please use FutureBuilder for this kind of usage. In your code i am not sure where you are changing state of isLoading flag.

So please update your question, and please be sure that you are changing isLoading flag in proper scope.

Please refer this for implementation of FutureBuilder

like image 145
Devarsh Ranpara Avatar answered Mar 05 '23 11:03

Devarsh Ranpara