简体   繁体   English

Flutter api 数据未显示在小部件中

[英]Flutter api data not showing in a widget

I have a simple page which is fetching data from an api.我有一个简单的页面,它从 api 获取数据。 Issue is data is fetching sucessfully but not showing in a widget.问题是数据已成功获取但未显示在小部件中。 Its showing sometime but majority time its not showing.它有时会显示,但大多数时候不会显示。 What i want is when user come or back on this page api will hit and show data in widget.我想要的是当用户访问或返回此页面时 api 将点击并在小部件中显示数据。

class AddressPage extends StatefulWidget {
  @override
  _AddressPageState createState() => _AddressPageState();
}

class _AddressPageState extends State<AddressPage> {
  var address = {'Address': []};
  bool showAddress = false;
  int dateindex = 0;
  var addressSelected;

  @override
  void initState() {
    setState(() {
      getAddress();
    });
  }

  getAddress() async {
    final storage = new FlutterSecureStorage();

    String _userEmail = await storage.read(key: "_userEmail");
    String _userPassword = await storage.read(key: "_userPassword");
    String url =
        'http://retailapi.airtechsolutions.pk/api/customer/login/${_userEmail}/${_userPassword}';

    print(url);
    http.Response res = await http.get(
      url,
    );
    var data = json.decode(res.body.toString());
    print(data);

    if (data['description'].toString() == "Success") {
      print(data['customer']['Addresses']);
      address['Address'].addAll(data['customer']['Addresses']);

      print(address);

      setState(() {

        showAddress == true;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: buildAppBar(context),
      body: Container(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            showAddress
                ? Flexible(
                    flex: 9,
                    child: SingleChildScrollView(
                      padding: EdgeInsets.symmetric(horizontal: 18.0),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          SizedBox(height: 20.0),
                          Text(
                            'order.shippingaddress',
                            style: Theme.of(context).textTheme.headline4,
                          ).tr(),
                          SizedBox(height: 20.0),
                          ListView.builder(
                            itemCount: address['Address'].length,
                            shrinkWrap: true,
                            scrollDirection: Axis.vertical,
                            physics: ScrollPhysics(),
                            itemBuilder: (context, index) {
                              return SideInAnimation(
                                index,
                                child: GestureDetector(
                                  // onTap: widget.onPressed,
                                  onTap: () {
                                    setState(() {
                                      dateindex = index;
                                      addressSelected =
                                          address['Address'][index];
                                      print(addressSelected);
                                    });
                                  },
                                  child: Container(
                                    width: double.infinity,
                                    padding: EdgeInsets.all(15.0),
                                    margin: EdgeInsets.only(bottom: 15.0),
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(12.0),
                                      border: Border.all(
                                        color: dateindex == index
                                            ? Theme.of(context).primaryColor
                                            : Theme.of(context).accentColor,
                                        width: dateindex == index ? 2.0 : 1.0,
                                      ),
                                    ),
                                    child: Column(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: [
                                        Text(
                                            showAddress
                                                ? address['Address'][index]
                                                    ['Address']
                                                : '...',
                                            style: Theme.of(context)
                                                .textTheme
                                                .headline4),
                                        SizedBox(height: 8.0),
                                        Text(
                                            showAddress
                                                ? address['Address'][index]
                                                    ['Address']
                                                : '...',
                                            style: Theme.of(context)
                                                .textTheme
                                                .subtitle1),
                                        SizedBox(height: 8.0),
                                        Text(
                                            showAddress
                                                ? address['Address'][index]
                                                    ['Address']
                                                : '...',
                                            style: Theme.of(context)
                                                .textTheme
                                                .subtitle1),
                                        SizedBox(height: 8.0),
                                        Row(
                                          children: [
                                            SizedBox(
                                              width: 80.0,
                                              child: RaisedButtonWidget(
                                                title: 'product.edit',
                                                onPressed: () {
                                                  Get.to(AddAddressPage());
                                                },
                                              ),
                                            ),
                                            SizedBox(width: 15.0),
                                            IconButton(
                                              icon: Icon(Icons.delete_outline),
                                              onPressed: () {
                                                // showDeleteConfirmation(context);
                                              },
                                            ),
                                          ],
                                        )
                                      ],
                                    ),
                                  ),
                                ),
                              );
                            },
                          ),
                          SizedBox(height: 25.0),
                        ],
                      ),
                    ),
                  )
                : Container(),
            buildConfirmAddressButton(),
          ],
        ),
      ),
    );
  }

  Flexible buildConfirmAddressButton() {
    return Flexible(
      flex: 1,
      child: Padding(
        padding: EdgeInsets.fromLTRB(18.0, 0.0, 18.0, 15.0),
        child: FadeInAnimation(
          2,
          child: RaisedButtonWidget(
            title: 'order.next',
            onPressed: navigateToPaymentPage,
          ),
        ),
      ),
    );
  }

  void navigateToPaymentPage() {
    Get.to(PaymentPage());
  }

  AppBar buildAppBar(BuildContext context) {
    return AppBar(
      centerTitle: true,
      title: Text(
        'order.checkout',
        style: Theme.of(context).textTheme.headline4,
      ).tr(),
      actions: [
        IconButton(
          icon: Icon(Icons.add),
          color: Theme.of(context).primaryColor,
          onPressed: () {
            Get.to(AddAddressPage());
          },
        ),
      ],
    );
  }
}

Dont get point why its not showing in a widget and why some time its showing:D i am so confused不要明白为什么它没有显示在小部件中以及为什么有时它会显示:我很困惑

You wrote:你写了:

setState(() {
  showAddress == true;
});

instead of代替

setState(() {
  showAddress = true;
});

Double equals are used to check 2 values.双等号用于检查 2 个值。
Simple equal is used to assign a value.简单相等用于赋值。

:) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM