0

I have a simple page which is fetching data from an 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.

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

1
  • Have you tried using a callBack as you said, API must hit when a user come back to this page? Commented Jan 25, 2021 at 15:43

1 Answer 1

3

You wrote :

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

instead of

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

Double equals are used to check 2 values.
Simple equal is used to assign a value.

:)

Sign up to request clarification or add additional context in comments.

3 Comments

yeah :D can you please tell also how can i call api function ( getAddress() )when i come page from other page ?
Hum, Maybe with didDependenciesUpdate / DidWidgetUpdate something like that. But, i need more context : I think your first page make an api call, you pushed some routes on him and when leaving these routes you would see updated data right ?
You can maybe listen navigator with this post i think : stackoverflow.com/questions/55632161/… It could respond to your problem maybe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.