Can not get data from a network request in a method that uses Scaffold.of(context)

130 Views Asked by At

I have two class methods inside my Stateful widget. As you can see one method is called inside the other one.

1st class method

Future<void> _getExchangeHousesList() async {
    const url = ApiEndpoints.getExchangeHouses;

    Map<String, String> requestBody = {
      'country_code': _userCountryCode,
      'lat': _userLocation.latitude.toString(),
      'long': _userLocation.longitude.toString(),
      'currency_code': 'USD',
      'entered_amount': '100',
    }; 

    final response = await http.post(
      Uri.parse(url),
      body: requestBody,
    );

    if (response.statusCode == 200) {
      final body = json.decode(response.body);
      final exchangeHousesList = body['data']['exchangehouse'] as List;

      for (var exchangeHouse in exchangeHousesList) {
        var exchangeHouseModal = ExchangeHouse.fromJson(exchangeHouse);
        _housesList.add(exchangeHouseModal);
      }
    }
  }  

2nd class method

void _getBestExchangeHouses(context, screenHeight, screenWidth) async {
    setState(() => _showFindHouseModal = false);

    // Prepare data for the network request
    final searchForPriceData = SearchForPrices(
      countryCode: _userCountryCode,
      currencyCode: _selectedCurrency.currencyCode,
      enteredAmount: _enteredAmount,
      userLatitude: _userLocation.latitude,
      userLongitude: _userLocation.longitude,
    );

    // Send the network request
    await _getExchangeHousesList();    // <----------------- First class method is called in here

    // Below code is responsible for rendering the bottom sheet once the data is fetched.
    const double sheetRadius = 35;

    await Scaffold.of(context)
        .showBottomSheet(
          (context) => ExchangeHousesSheet(
            height: screenHeight * 0.6,
            width: screenWidth,
            borderRadius: sheetRadius,
            housesList: _housesList,
          ),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(sheetRadius),
          ),
        )
        .closed;

    // Below code does clean up after the bottom sheet is closed
    _enteredAmountController.text = ''; // Clears the entered amount in the find house modal after the sheet is closed
    setState(() => _showFindHouseModal = true);
  }  

As I have pointed inside the second class method, I am calling the first class method in that second class method.

HOWEVER calling that first class method inside the second class method gives me the following error,

E/flutter (22176): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.
E/flutter (22176): At this point the state of the widget's element tree is no longer stable.
E/flutter (22176): To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.
E/flutter (22176): #0      Element._debugCheckStateIsActiveForAncestorLookup.<anonymous closure> (package:flutter/src/widgets/framework.dart:4032:9)
E/flutter (22176): #1      Element._debugCheckStateIsActiveForAncestorLookup (package:flutter/src/widgets/framework.dart:4046:6)
E/flutter (22176): #2      Element.findAncestorStateOfType (package:flutter/src/widgets/framework.dart:4093:12)
E/flutter (22176): #3      Scaffold.of (package:flutter/src/material/scaffold.dart:2144:43)
E/flutter (22176): #4      _AppHomeScreenState._getBestExchangeHouses (package:lanka_remittance/screens/main_screens/app_home_screen.dart:179:20)
E/flutter (22176): <asynchronous suspension>
E/flutter (22176): 

I know that this is related to me calling Scaffold.of(context) inside the second class method. I can not figure out how to solve this though. (I have also come across posts where people mention to pass a scaffold key. I tried that but it did not work. I think that I might have passed the scaffold key wrong)

Can you please help me fix this?

0

There are 0 best solutions below