2

I'm using Flutter with GetX for state management. I have a WebView inside a Stack, and I want to display a CircularProgressIndicator while the WebView is loading. However, the Obx widget does not seem to react to updates in the loading state.

Here is the relevant part of my code:

class WebViewPage extends StatefulWidget {
  @override
  _WebViewPageState createState() => _WebViewPageState();
}

class _WebViewPageState extends State<WebViewPage> {
  late InAppWebViewController webViewController;
  final WebController _controller = Get.put(WebController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarWidget(),
      endDrawer: Drawer(),
      body: _buildMobileWebView(),
    );
  }

  Widget _buildMobileWebView() {
    return Stack(
      children: [
        VerticalSpacing(10),
        InAppWebView(
          initialUrlRequest: URLRequest(
              url: WebUri(_controller.currentUrl.string)), // Initial URL
          initialSettings: InAppWebViewSettings(
            javaScriptEnabled: true,
            useShouldOverrideUrlLoading: true,
            mediaPlaybackRequiresUserGesture: false,
          ),
          onWebViewCreated: (InAppWebViewController controller) {
            webViewController = controller;
          },
          onLoadStart: (controller, url) {
            _controller.updateLoading(true); // Start loading
            _controller.updateUrl(url.toString());
            // Update URL
          },
          onLoadStop: (controller, url) async {
            _controller.updateLoading(false); // Stop loading
            _controller.updateUrl(url?.toString() ?? ''); // Update URL
          },
          onProgressChanged: (controller, progress) {
            if (progress < 100) {
              _controller
                  .updateLoading(true); // Show loading until it's complete
            } else {
              _controller.updateLoading(false); // Stop loading at 100%
            }
          },
        ),
        Obx(() {
          return _controller.isLoading.value
              ? Center(child: CircularProgressIndicator())
              : SizedBox.shrink();
        }),
      ],
    );
  }
}

class WebController extends GetxController {
  var isLoading = true.obs;
  var currentUrl = 'https://mpfsts.mp.gov.in/'.obs;

  void updateLoading(bool loading) {
    print('Loading state updated: $loading');
    isLoading.value = loading;
  }

  void updateUrl(String url) {
    currentUrl.value = url;
    print("Susheel: ${currentUrl.value}");
  }
}

Problem: The Obx widget doesn't seem to react to changes in the isLoading observable when calling _controller.updateLoading(true/false) during WebView load events. The CircularProgressIndicator is not showing up as expected.

What I've Tried: Checked if the isLoading state is being updated correctly using print statements. Ensured that Get.put(WebController()) is only initialized once. Verified that Obx correctly listens to isLoading.value. Question: What am I missing here? Why is the Obx widget not reflecting the changes in the loading state when updating via updateLoading()?

3
  • I can't find any mistakes. Maybe also try printing inside the Obx to see if a rebuild is triggered? Commented Oct 15, 2024 at 13:11
  • idk is it possible that the cause is stateful try to switch to stateless because your code seem to be okay Commented Oct 16, 2024 at 3:02
  • code is fine try with Getview, Stateless or debug the cod eagain Commented Oct 17, 2024 at 7:58

1 Answer 1

0

While I cannot identify the specific problem in your code that leads to wrong behavior, your design has two huge bugs.

  1. You are using stateful widgets. One of GetX's biggest advantages is that it allows us to avoid StatefulWidgets completely.

  2. You don't use Binding to instantiate GetxController.

Follow those guidelines and GetX will make you happy for the rest of your developer life.

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

Comments

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.