0

我的Stateful小部件中有两个类方法。如您所见,在另一种方法中调用了一种方法

一级方法

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);
      }
    }
  }  

二级方法

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);
  }  

正如我在第二类方法中指出的那样,我在第二类方法中调用了第一类方法。

但是在第二类方法中调用第一类方法会给我以下错误,

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): 

我知道这与我Scaffold.of(context)在第二类方法中调用有关。我不知道如何解决这个问题。(我也遇到过人们提到传递脚手架密钥的帖子。我尝试过但没有用。我认为我可能错误地传递了脚手架密钥)

你能帮我解决这个问题吗?

4

0 回答 0