-1

i use woocommerce rest api with flutter to get product variations. Woocommerce rest api is too slowly to get this variations. I need to send a message to the user to wait for the process to finish. How to put this message in the code?

     @override
  Future<List<ProductVariation>> getProductVariations(Product product,
      {String lang = 'en'}) async {
    try {
      final List<ProductVariation> list = [];
      int page = 1;

      while (true) {
        String endPoint =
            "products/${product.id}/variations?per_page=100&page=$page";
        if (kAdvanceConfig["isMultiLanguages"]) {
          endPoint += "&lang=$lang";
        }

        var response = await wcApi.getAsync(endPoint);
        if (response is Map && isNotBlank(response["message"])) {
          throw Exception(response["message"]);
        } else {
          if (response is List && response.isEmpty) {
            /// No more data.
            break;
          }
          for (var item in response) {
            if (item['visible']) {
              list.add(ProductVariation.fromJson(item));
            }
          }

          /// Fetch next page.
          page++;
        }
      }

      return list;
    } catch (e) {
      //This error exception is about your Rest API is not config correctly so that not return the correct JSON format, please double check the document from this link https://docs.inspireui.com/fluxstore/woocommerce-setup/
      rethrow;
    }
  }

Any help?

1
  • Just want to confirm. You want some kind of a toast or loading widget to inform user that request is still ongoing?
    – rickimaru
    Commented Jan 19, 2021 at 9:42

1 Answer 1

1

Refer to this small example:

class Waitscreen extends StatefulWidget {
      Waitscreen ({Key key}) : super(key: key);
    
      @override
      _WaitscreenState createState() => _WaitscreenState();
    }
    
    class _WaitscreenState extends State<Waitscreen> {
      bool _isLoading = false;

    @override
    Widget build(BuildContext context) {
     return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: RaisedButton(
        child: Text(_isLoading ? "Loading..." : "Load"),
          onPressed: () async {
            setState((){_isLoading = !_isLoading;});
            // TODO
            await Future.delayed(Duration(seconds: 5)); // await getProductVariations...
            // TODO
            setState((){_isLoading = !_isLoading;});
        }
        ),
      ),
    );
   } 
 }

Then you can do something like, according to your need!

3

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.