如果我的列表为空,如何显示图像?

Jor*_*den 5 android json ios dart flutter

我想在中心显示图像或文本,就像No Record Found所有详细信息都是从 API 获取的一样。在本机 Android 中,setEmptyView()对于这种情况,但不知道如何在 flutter 中做到这一点。不间断进度 对话框正在运行,但不显示文本。我在 JSON 响应中添加了代码

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

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

    )
        : Center(child: Text("No Found"));
  }
Run Code Online (Sandbox Code Playgroud)
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;
  }
Run Code Online (Sandbox Code Playgroud)
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",
    }
]

Run Code Online (Sandbox Code Playgroud)

Cra*_*Cat 1

尝试这个,

import 'dart:convert';

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

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() => runApp(MaterialApp(home: MyWidget()));

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  Future<List<NewAddressModel>> newAddress;

  @override
  void initState() {
    newAddress = hitGetAddressApi();
    super.initState();
  }

  Future<List<NewAddressModel>> hitGetAddressApi() async {
    final response = await http.post("api");
    if (response.statusCode == 200) {
      final responseBody = json.decode(response.body);
      if (responseBody is List)
        return responseBody
            .map((data) => new NewAddressModel.fromJson(data))
            .toList();
      else {
        print(responseBody);
        return null;
      }
    } else {
      print(response.statusCode);
      throw Exception("Problem in fetching address List");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder<List<NewAddressModel>>(
        future: newAddress,
        builder: (context, snapShot) {
          if (snapShot.connectionState == ConnectionState.waiting)
            return Center(
              child: CircularProgressIndicator(),
            );
          else if (snapShot.hasError) {
            return Center(
              child: Text("ERROR: ${snapShot.error}"),
            );
          } else {
            if (snapShot.hasData && snapShot.data.isNotEmpty)
              return getListView(snapShot.data);
            else //`snapShot.hasData` can be false if the `snapshot.data` is null
              return Center(
                child: Text("No Data Found"),
              );
          }
        },
      ),
    );
  }

  Widget getListView(List<NewAddressModel> addressList) {
    return ListView.builder(
      itemCount: addressList.length,
      itemBuilder: (BuildContext context, int index) {
        final address = addressList[index];
        //Change `addressViewCard` to accept an `NewAddressModel` object
        return addressViewCard(address);
      },
    );
  }

  Widget addressViewCard(NewAddressModel address) {
    //implement based on address instead of index
    return ListTile(title: Text("${address.address}"));
  }
}

class NewAddressModel {
  String id;
  String name;
  String mobile;
  String address;
  String state;
  String city;
  String pin;
  String userid;

  NewAddressModel({
    this.id,
    this.name,
    this.mobile,
    this.address,
    this.state,
    this.city,
    this.pin,
    this.userid,
  });

  factory NewAddressModel.fromJson(Map<String, dynamic> json) =>
      NewAddressModel(
        id: json["id"],
        name: json["name"],
        mobile: json["mobile"],
        address: json["address"],
        state: json["state"],
        city: json["city"],
        pin: json["pin"],
        userid: json["userid"],
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "name": name,
        "mobile": mobile,
        "address": address,
        "state": state,
        "city": city,
        "pin": pin,
        "userid": userid,
      };
}
Run Code Online (Sandbox Code Playgroud)