首页 > 解决方案 > 在 null 上调用 getter

问题描述

我正在尝试显示进度条,直到获取数据。在初始化状态下,我调用了 api。如图所示,它显示错误,然后显示布局而不显示进度条。我是新来的,无法通过这个。

在此处输入图像描述

我已经实现如下:

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

获取详细信息方法

void getDetails() async {
setState(() {
  _isLoading = true;
});

MenuDetailsResponse moreResponse = await getMenuDetails(widget.id);

if (moreResponse != null && moreResponse.data != null) {
  setState(() {

    details = moreResponse.data;
    if (details.detail.maxQty != null) {
      maxQtyController =
          new TextEditingController(text: details.detail.maxQty.toString());
    } else {
      maxQtyController = new TextEditingController();
    }
    print(details);
    _isLoading = false;
  });
} else if (moreResponse != null) {
  setState(() {
    _isLoading = false;
  });
  showAlerts(context, "Sorry!!", moreResponse.message, AlertType.error);
} else {
  setState(() {
    _isLoading = false;
  });
  showAlerts(context, "Sorry!!",
      "Something went wrong, Please try again later!!", AlertType.error);
}
}

构建方法:

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        iconTheme: IconThemeData(color: Theme.of(context).primaryColor),
        title: Text(
          "Menu Details",
          style: TextStyle(color: Theme.of(context).primaryColor),
        ),
      ),
      body: Stack(
        children: <Widget>[
          Opacity(
            opacity: _isLoading
                ? 0.3
                : 1, // You can reduce this when loading to give different effect
            child: AbsorbPointer(
              absorbing: _isLoading,
              child: _buildLayout(),
            ),
          ),
          Opacity(
              opacity: _isLoading ? 1.0 : 0,
              child: Center(
                child: CircularProgressIndicator(
                  backgroundColor: Theme.of(context).primaryColor,
                ),
              )),
        ],
      ),
    );
  }

BuildLayout 小部件

    Widget _buildLayout() {
        return Container(
          margin: EdgeInsets.all(10),
          child: Wrap(
            children: <Widget>[
              Column(
                children: <Widget>[
                  SizedBox(height: 20),
                  Text(
                    "Enter max quantity for today",
                    style: TextStyle(color: Theme.of(context).primaryColor),
                  ),
                  SizedBox(height: 20),
                  _buildTopItems(),           
                ],
              )
            ],
          ),
        );
      }

小部件 topItems

    Widget _buildTopItems() {
    return Container(
      margin: EdgeInsets.only(top: 10),
      child: Wrap(
        children: <Widget>[
          Row(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Container(
                  child: Image.network(
                    details.detail.image,
                    height: 150,
                    width: 150,
                    fit: BoxFit.fill,
                  ),
                ),
                SizedBox(
                  width: 10,
                ),
                Expanded(
                    child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      details.detail.name,
                      style: TextStyle(fontSize: 18),
                    ),
                    SizedBox(height: 5),
                    Row(
                      children: <Widget>[
                        Text("Rs " + details.detail.price.toString(),
                            style: TextStyle(
                                decoration: TextDecoration.lineThrough)),
                        SizedBox(width: 5),
                        Text("Rs " + details.detail.discountPrice.toString(),
                            style: TextStyle(
                              fontSize: 17,
                              color: Theme.of(context).primaryColor,
                            ))
                      ],
                    ),
                    SizedBox(height: 5),
                    Text(details.detail.foodtypedata.foodType),
                    SizedBox(height: 5),
                    StarRating(
                      rating: double.parse(details.detail.rating),
                      size: 24.0,
                    ),
                    SizedBox(height: 5),
                    details.detail.status >= 1
                        ? Text(
                            "Available",
                            style: TextStyle(
                                color: Theme.of(context).primaryColor),
                          )
                        : Text(
                            "UnAvailable",
                            style: TextStyle(color: Colors.red),
                          ),
                    Text(
                      "-  " + details.detail.createdAt,
                      textAlign: TextAlign.left,
                    ),
                  ],
                ))
              ]),
        ],
      ),
    );
  }

标签: flutter

解决方案


我通过如下改变 buildmethod 解决了这个问题:

    @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.white,
          iconTheme: IconThemeData(color: Theme.of(context).primaryColor),
          title: Text(
            "Menu Details",
            style: TextStyle(color: Theme.of(context).primaryColor),
          ),
        ),
        body: _isLoading
            ? Center(
                child: CircularProgressIndicator(
                  backgroundColor: Theme.of(context).primaryColor,
                ),
              )
            : _buildLayout()
        );
  }

推荐阅读